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.
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/terminate करती हैं।
Syntax Errors
Logical Errors
Run Time Errors
●●●
C++ में Syntax Errors program में syntax सही न होने के कारण आती हैं , इन्हे compile time error भी कहते हैं जो कि compile time पर आती हैं।
For Example -
#include <iostream>
int main() {
// here we forgot to use semicolon;
std::cout << "Hello world!"
return 0;
}
error: expected ';' before 'return'
ये errors program में होने पर हम , program को run करना तो दूर compile भी नहीं कर सकते हैं।
●●●
Logical Errors , program में apply किये गए logic में mistake की वजह से आती हैं। इन Logical Errors की वजह से हमें desirable output नहीं मिलता। Logical Errors को बिना किसी tool के handle करना मुश्किल है।
For Example -
#include <iostream>
using namespace std;
int main() {
// here we have put semicolon just after the for loop
for(int i=1; i<=5; i++);{
cout << "It will print only once";
}
return 0;
}
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 हैं।
#includeusing namespace std; int main() { for(int i=0; i<=5; i--) { cout << echo "Hello"; } return 0; } // Logical Error : This loop will never end..
●●●
Runtime Errors , program के running time पर आती हैं , run time errors को ही exceptions कहते हैं। और सिर्फ इन्ही errors को हम handle कर सकते हैं।
जब भी program में Run Time Error / Exception आती है , तो C++ इन errors का Object create करती है।Run Time Error / Exception को handle करने के लिए JavaScript हमें कुछ important statements provide कराती है - जिन्हे हम next topic में discuss करेंगे ।
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