0% found this document useful (0 votes)
1 views2 pages

Java Exception Handling Complete Notes

Uploaded by

animecreate95
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)
1 views2 pages

Java Exception Handling Complete Notes

Uploaded by

animecreate95
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 - COMPLETE

MERGED NOTES

1. What is Exception?

English: An exception is an unwanted event that occurs during program execution and disrupts the
normal flow of the program.
Marathi: Exception ■■■■■■ program ■■■■ ■■■■■■ ■■■■■■ ■■■■■■■■■ ■■■■ ■■
program ■■ flow disturb ■■■■.

Example:
int a = 10;
int b = 0;
int result = a / b; // ArithmeticException

2. try-catch-finally Keywords

try: Wrap risky code.


catch: Handle exception object.
finally: Always executes for cleanup.

Example:
try {
int a = 10 / 0;
}
catch (ArithmeticException e) {
[Link]("Handled");
}
finally {
[Link]("Always runs");
}

3. Checked vs Unchecked Exceptions

Checked: Compile-time checked (e.g., IOException). Must handle or declare using throws.
Unchecked: Runtime exceptions (e.g., ArithmeticException, NullPointerException). Not mandatory
to handle.

Checked Example:
FileReader file = new FileReader("[Link]"); // must handle

Unchecked Example:
int x = 10 / 0; // runtime error

4. Custom Exception
Custom Exception is created by extending Exception or RuntimeException to handle business logic
errors.

class InvalidAgeException extends Exception {


InvalidAgeException(String msg) {
super(msg);
}
}

static void checkAge(int age) throws InvalidAgeException {


if(age < 18) {
throw new InvalidAgeException("Age must be 18+");
}
}

5. JVM Internal Working (Stack + Heap)

When exception occurs, JVM creates exception object in Heap memory and searches for handler in
Stack memory.
If no handler found, stack unwinding happens and program terminates.

Stack Memory: Stores method calls


Heap Memory: Stores objects (including exception objects)

Process:
1. Exception occurs
2. Object created in heap
3. JVM searches catch block
4. Stack unwinding
5. Program continues or terminates

6. Error vs Exception

Error: Serious system-level problem (e.g., OutOfMemoryError). Not recoverable.


Exception: Runtime problem that can be handled.

END OF COMPLETE NOTES

You might also like