If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
जब हम किसी program को implement करते हैं किसी project पर काम करते हैं तो जरूरी नहीं वो logic / code 100% सही हो। मतलब हो सकता है आपके code में कुछ error आए। इस topic में exceptions के बारे में पढ़ेंगे।
Exception Handling , program में आयी unwanted errors को handle करने का एक process / method है। इन unwanted errors की वजह से कभी कभी program terminate हो जाता है। हालाँकि program का terminate होना Exception
पर depend करता है कि Exception किस तरह की है।
●●●
Understanding Exception : Exceptions कुछ unwanted events होती हैं , जो program execution के समय आती हैं और program को disrupts करती हैं।
Normally errors 3 तरह की होती है -
Syntax Errors
Logical Errors
Run Time Errors
Syntax Errors program में syntax सही न होने के कारण आती हैं , किसी भी programming में Syntax errors के रहते आप program को execute नहीं कर सकते हैं ।
क्योंकि अगर program का syntactically ही सही नहीं होगा तो interpreter use execute कैसे करेगा।
Example के लिए नीचे दिए गए program में कई जगह semicolon ;
या curly brackets ही नहीं है तो obviously ये program execute नहीं होगा।
Example -
function sayHi() {
echo "Hey ! learners"
}
sayHi()
PHP Parse error: syntax error, unexpected '}', expecting ',' or ';'
Note* Syntax Errors को किसी tool या code से resolve नहीं किया जा सकता है , ये developer पर depend करता है कि code कैसा लिखा जाये।
●●●
Logical Errors , program में apply किये गए logic में mistakes की वजह से आती हैं। इन Logical Errors की वजह से हमें desirable output नहीं मिलता। Logical Errors को बिना किसी tool के handle करना मुश्किल है।
For example -
for($i=1; $i<=5; $i++);{
echo "It will print only once";
}
// It will print only once.
अब ऊपर दिया गया example में तो हमने 5 बार iterate करने के लिए code लिखा।
लेकिन हमने for loop के just बाद semicolon ;
लगा दिया है तो syntactically हमारा program सही है , but programmatically हमें desired output नहीं मिलेगा , क्योंकि loop तो पहले ही close कर दिया है।
किसी Recursive Functions , for Loop और while Loop में ऐसी condition रखना जो कभी false ही न हो , जिससे program infinite run होता रहेगा , ये कुछ Logical Error के examples हैं।
for($i=0; $i<=5; $i--) {
echo "Hello";
}
This loop will never end..
●●●
Runtime Errors , program के running time पर आती हैं , run time errors को ही Exceptions
कहते हैं। और सिर्फ इन्ही errors को हम handle कर सकते हैं।
Exceptions को handle करने या फिर manually किसी exception को generate करने के लिए PHP में नीचे दिए गए statements use किये जाते है।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook