0% found this document useful (0 votes)
3 views7 pages

Java Exception Handling Notes

This document provides an overview of Java Exception Handling, explaining what exceptions are and why handling them is important. It covers the use of try-catch blocks, common exceptions, specific catch blocks, multiple catch blocks, the finally block, and the throw keyword for manually creating exceptions. The document includes examples and outputs to illustrate each concept clearly.

Uploaded by

harmankatnour01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Java Exception Handling Notes

This document provides an overview of Java Exception Handling, explaining what exceptions are and why handling them is important. It covers the use of try-catch blocks, common exceptions, specific catch blocks, multiple catch blocks, the finally block, and the throw keyword for manually creating exceptions. The document includes examples and outputs to illustrate each concept clearly.

Uploaded by

harmankatnour01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like