जब program execution के वक्त कोई problem आ जाता है, तो उसे Exception कहते है |
Exception ये runtime पर आता है |
Java में दो प्रकार के Errors होते है |
Run-Time Error : यहाँ Program successfully run होता है | लेकिन कुछ ऐसी internal errors आ जाती है, जो interpreter द्वारा दी जाती है | इससे Program भी बंद हो जाता है |
Exception के बारे में देखे तो, जब Program compile-time पर successfully run होता है, तो program में कुछ statements ऐसे होते कि वो Compiler द्वारा execute नहीं होते है, उसी वक्त interpreter द्वारा उस statements के सम्बंधित वो एक object create करता है, जब object create किया जाता है, तब interpreter द्वारा Run-Time पर उसे throw किया जाता है |
इससे ये समझ आता है कि, जब भी कभी ऐसी condition program में आती है, तब Exception Handling का इस्तेमाल Program में किया जाता है |
Error : जब Program में Syntax error, format error या program; out of memory जाता है, तो Error आ जाती है | ये errors; critical होती है | इन error को ढूंढना काफी कठिन होता है | ये Error Compile-time पर आ जाता है |
ऊपर के program में देखे तो ये Run-Time पर ArithmeticException ये Exception occur होता है | ये एक User का ख़राब Programming का उदाहरण है | यहाँ पर Runtime पर User को warning दी जाती है | Program पर आया हुआ Exception काफी अच्छे से समझ आता है |
Program में Exception Handling में दो प्रकार के काम करता है |
IOException
ClassNotFoundException आदि.
Program में देखे तो FileInputStream data को read करने के लिए इस्तेमाल किया गया है | FileInputStream से FileNotFoundException; ये Exception occur होता है | ये सब compile-time पर होता है और Programmer को Exception handle करने के लिए बताया जाता है |
Source Code :
निचे दिया हुआ program Exception Handling का है | जब कोई file found नहीं होती तो एक message display होता है | Exception Handling के लिए try, catch का इस्तेमाल किया गया है |
Source Code :
Un-checked Exception : Un-checked Exception ये run time पर occur होता है | इसे Run-time Exception भी कहा जाता है | इसे compile-time पर check नहीं किया जाता |जब इसे programmer द्वारा इसे handle नहीं किया जाता तो JVM इसे handle कर देता है |
For Example,
ArithmeticException
NullPointerException
NumberFormatException आदि.
Source Code :
Exception Handling के लिए java में पांच keywords का इस्तेमाल किया जाता है |
Exception ये runtime पर आता है |
Java में दो प्रकार के Errors होते है |
- Compile-Time Error
- Run-Time Error
Run-Time Error : यहाँ Program successfully run होता है | लेकिन कुछ ऐसी internal errors आ जाती है, जो interpreter द्वारा दी जाती है | इससे Program भी बंद हो जाता है |
Exception के बारे में देखे तो, जब Program compile-time पर successfully run होता है, तो program में कुछ statements ऐसे होते कि वो Compiler द्वारा execute नहीं होते है, उसी वक्त interpreter द्वारा उस statements के सम्बंधित वो एक object create करता है, जब object create किया जाता है, तब interpreter द्वारा Run-Time पर उसे throw किया जाता है |
इससे ये समझ आता है कि, जब भी कभी ऐसी condition program में आती है, तब Exception Handling का इस्तेमाल Program में किया जाता है |
Exception और Error का फर्क
Exception : Exception Run-time पर आ जाता है , जब Program; में divided by zero पर Exception आ जाता है |Error : जब Program में Syntax error, format error या program; out of memory जाता है, तो Error आ जाती है | ये errors; critical होती है | इन error को ढूंढना काफी कठिन होता है | ये Error Compile-time पर आ जाता है |
Check Example for Normal Exception
Source Code :
class Sample{
public static void main(String args[]){
int a = 4/0;
}
}
Output :
C:\Program Files\Java\jdk1.8.0_111\bin>javac Sample.java
C:\Program Files\Java\jdk1.8.0_111\bin>java Sample
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Sample.main(Sample.java:5)
Describing Output
ऊपर के program में देखे तो ये Run-Time पर ArithmeticException ये Exception occur होता है | ये एक User का ख़राब Programming का उदाहरण है | यहाँ पर Runtime पर User को warning दी जाती है | Program पर आया हुआ Exception काफी अच्छे से समझ आता है |
Java Exception Classes
- Checked Exception
- Un-checked Exception
IOException
ClassNotFoundException आदि.
Program में देखे तो FileInputStream data को read करने के लिए इस्तेमाल किया गया है | FileInputStream से FileNotFoundException; ये Exception occur होता है | ये सब compile-time पर होता है और Programmer को Exception handle करने के लिए बताया जाता है |
Source Code :
import java.io.FileInputStream;
class Sample{
public static void main(String args[]){
FileInputStream file = null;
file = new FileInputStream("file.txt");
}
}
Output :
C:\Program Files\Java\jdk1.8.0_111\bin>javac Sample.java
Sample.java:7: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
file = new FileInputStream("file.txt");
^
1 error
निचे दिया हुआ program Exception Handling का है | जब कोई file found नहीं होती तो एक message display होता है | Exception Handling के लिए try, catch का इस्तेमाल किया गया है |
Source Code :
import java.io.FileInputStream;
import java.io.FileNotFoundException;
class Sample{
public static void main(String args[]){
FileInputStream file = null;
try{
file = new FileInputStream("file.txt");
}
catch(FileNotFoundException e){
System.out.println("File is not Found.");
}
}
}
Output :
File is not Found.
Un-checked Exception : Un-checked Exception ये run time पर occur होता है | इसे Run-time Exception भी कहा जाता है | इसे compile-time पर check नहीं किया जाता |जब इसे programmer द्वारा इसे handle नहीं किया जाता तो JVM इसे handle कर देता है |
For Example,
ArithmeticException
NullPointerException
NumberFormatException आदि.
Source Code :
class Sample{
public static void main(String args[]){
int a = 4/0;
}
}
Output :
C:\Program Files\Java\jdk1.8.0_111\bin>javac Sample.java
C:\Program Files\Java\jdk1.8.0_111\bin>java Sample
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Sample.(Sample.java:3)
at Sample.main(Sample.java:6)
Exception Handling के लिए java में पांच keywords का इस्तेमाल किया जाता है |
- try
- catch
- finally
- throw
- throws







No comments: