जब program execution के वक्त कोई problem आ जाता है, तो उसे Exception कहते है |
Exception ये runtime पर आता है |
Java में दो प्रकार के Errors होते है |
- Compile-Time Error
- Run-Time Error
Compile-Time Error : ये एक normal Errors है जो Compiler द्वारा आ जाता है | जब Program में कही syntax में, कही curly brace, semi-colon या comma नहीं दिया आता है, तो ये Compile-Time पर Error occur होता है |
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 :Output :class Sample{ public static void main(String args[]){ int a = 4/0; } }
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


Program में Exception Handling में दो प्रकार के काम करता है |
- Checked Exception
- Un-checked Exception
Checked Exception : Checked Exception ये compile time पर occur होता है | इसे Compile-time Exception भी कहा जाता है | जब program में किसी प्रकार की checked exception आती है तो User को compiler द्वारा बताया जाता है कि आप उस Exception को handle करे |
For Example,
IOException
ClassNotFoundException आदि.
Program में देखे तो FileInputStream data को read करने के लिए इस्तेमाल किया गया है | FileInputStream से FileNotFoundException; ये Exception occur होता है | ये सब compile-time पर होता है और Programmer को Exception handle करने के लिए बताया जाता है |
Source Code :
Output :import java.io.FileInputStream; class Sample{ public static void main(String args[]){ FileInputStream file = null; file = new FileInputStream("file.txt"); } }
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 :Output :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."); } } }
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 :
Output :class Sample{ public static void main(String args[]){ int a = 4/0; } }
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 का इस्तेमाल किया जाता है |