Java Exception Handling
Core Java Notes | OOP | Handwritten Style
1. What is an Exception?
Exception = Runtime par aane wali error
Program execution ke time abnormal condition occur ho jaye.
>> Example : Divide by Zero
int a = 10;
int b = 0;
[Link](a / b); // <-- Error!
Output:
ArithmeticException: / by zero
--> Program crash ho jayega!
2. Why Exception Handling?
Without Handling With Handling
Error Error
Program STOP Handle Error
(Data Loss!) Program Continue!
Page 1
Java Exception Handling [ Continued ]
3. try-catch Block
>> Syntax
try
{
// risky code <-- jo fail ho sakta hai
}
catch(Exception e)
{
// handling code <-- error handle karo
}
>> Example 1 : Divide By Zero
public class Main
{
public static void main(String[] args)
{
try
{
int result = 10 / 0;
[Link](result);
}
catch(Exception e)
{
[Link]("Cannot Divide By Zero");
}
[Link]("Program Ended");
}
}
Output:
Cannot Divide By Zero
Program Ended
>> Example 2 : Array Index Error
int[] arr = {10, 20, 30};
try
{
[Link](arr[5]); // index 5 exist nahi karta!
}
catch(Exception e)
{
[Link]("Invalid Index");
}
Page 2
Output:
Invalid Index
4. Exception Object
catch(Exception e) --> "e" error ki saari information rakhta hai
>> Print Error using e
catch(Exception e)
{
[Link](e); // full exception
[Link]([Link]()); // sirf message
}
Output:
[Link]: / by zero
Useful Methods of Exception Object:
[Link]() --> Sirf error message milega
[Link]() --> Full exception name + message
[Link]() --> Complete error trace milega
Page 3
Java Exception Handling [ Continued ]
5. Common Exceptions
ArithmeticException
10 / 0 <-- Zero se divide karna
ArrayIndexOutOfBoundsException
arr[10] <-- Array ki range se bahar
NullPointerException
String s=null; [Link]() <-- Null object pe method call
InputMismatchException
[Link]() <-- "Hello" <-- Wrong input type in Scanner
NumberFormatException
[Link]("ABC") <-- String to number convert fail
6. Specific catch Block
Ek specific exception type ko catch karna:
try
{
int result = 10 / 0;
}
catch(ArithmeticException e) // Specific type!
{
[Link]("Math Error");
}
Output:
Math Error
7. Multiple catch Blocks
Alag-alag exceptions ko alag catch blocks mein handle karo:
Page 4
try
{
int[] arr = {10, 20};
[Link](arr[5]); // Array error
int x = 10 / 0; // Math error
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("Array Error");
}
catch(ArithmeticException e)
{
[Link]("Math Error");
}
NOTE: Generic Exception catch ALWAYS last mein likhna!
Pehle specific exceptions, phir generic (parent) exception.
Page 5
Java Exception Handling [ Continued ]
8. finally Block
* finally block ALWAYS execute hota hai!
Exception aaye ya na aaye -- finally zaroor chalega.
>> Syntax
try
{
// risky code
}
catch(Exception e)
{
// handling code
}
finally
{
// ALWAYS runs -- cleanup code yahan
// e.g. file / connection close karo
}
>> Example
try
{
int result = 10 / 0;
}
catch(Exception e)
{
[Link]("Error");
}
finally
{
[Link]("Always Runs");
}
Output:
Error
Always Runs
finally ka common use :
-> File close karna (after read/write)
-> Database connection close karna
-> Network / socket cleanup
Page 6
9. throw Keyword
throw = Manually / deliberately exception create karna
Jab hum khud decide karein ki exception kab hogi.
>> Basic Example
int age = 15;
if(age < 18)
{
throw new ArithmeticException();
}
>> Example with try-catch + Custom Message
int age = 15;
try
{
if(age < 18)
{
throw new Exception("Not Eligible");
}
}
catch(Exception e)
{
[Link]([Link]());
}
Output:
Not Eligible
Quick Reference Summary
try --> Risky code yahan daalo
catch(E e) --> Exception pakdo, e se info lo
finally --> Cleanup code -- hamesha run karega
throw --> Manually exception raise karo
[Link]() --> Exception ka message lo
Java Exception Handling Notes | End of Chapter | Study Hard!
Page 7