Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views5 pages

Exception Handling

The document provides a comprehensive overview of Java exception handling, detailing what exceptions are, their types, and the differences between errors and exceptions. It explains the exception hierarchy, keywords used in exception handling, and best practices for managing exceptions in Java code. Additionally, it includes examples of try-catch blocks, custom exceptions, and practice questions for further understanding.

Uploaded by

solomon masih
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)
8 views5 pages

Exception Handling

The document provides a comprehensive overview of Java exception handling, detailing what exceptions are, their types, and the differences between errors and exceptions. It explains the exception hierarchy, keywords used in exception handling, and best practices for managing exceptions in Java code. Additionally, it includes examples of try-catch blocks, custom exceptions, and practice questions for further understanding.

Uploaded by

solomon masih
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
You are on page 1/ 5

📘 Java Exception Handling Master Notes (Hindi-English Mix)

🔰 1. What is Exception?
Exception ek unwanted ya unexpected situation hoti hai jo program ke runtime par aati hai aur uska
normal flow disturb kar deti hai.
🔸 For example:
• Divide by zero
• File not found
• Array index out of bound
• Null pointer access
💡 Exception = “Error at runtime that can be handled.”
🔰 2. Types of Errors in Java
Type Description
Compile-time error Syntax error, missing semicolon etc. Detected by compiler.
Run-time error Occurs during execution. Eg: divide by 0.
Logical error Wrong output due to logic mistake. No exception generated.

🔰 3. Difference between Error and Exception


Feature Error Exception
Type Unrecoverable Recoverable
Cause JVM issues (memory, stack etc.) Code-related issues
Handling Cannot be handled Can be handled via try-catch
Example StackOverflowError NullPointerException

🔰 4. Exception Hierarchy (Java Class Tree)


javascript
CopyEdit
Object
|
Throwable
/ \
Error Exception
/ \
Checked Unchecked
🔰 5. Types of Exceptions
✅ Checked Exception (Compile-time):
• JVM forces you to handle.
• Example:
• IOException
• SQLException
• ClassNotFoundException

⚠️ Unchecked Exception (Runtime):


• JVM does NOT force you to handle.
• Example:
• ArithmeticException
• NullPointerException
• ArrayIndexOutOfBoundsException

🔰 6. Keywords used in Exception Handling


Keyword Description
try Risky code block here
catch Handles the exception
finally Always executes (cleanup)
throw Used to throw an exception manually
throws Declares exception for method caller

🔰 7. try-catch Block Syntax


java
CopyEdit
try {
// Risky code
} catch(ExceptionType e) {
// Handling code
}

Example:
java
CopyEdit
int a = 10, b = 0;
try {
System.out.println(a / b);
} catch(ArithmeticException e) {
System.out.println("Can't divide by zero!");
}

🔰 8. Multiple catch blocks


java
CopyEdit
try {
// code
} catch(ArithmeticException e) {
// handle AE
} catch(NullPointerException e) {
// handle NPE
} catch(Exception e) {
// handle any other
}

Catch blocks should go from most specific to most general.

🔰 9. finally Block
• Always executed (whether exception is thrown or not).
• Used for cleanup code like closing database, closing file etc.
java
CopyEdit
try {
int data = 25/5;
} catch(Exception e) {
System.out.println("Handled");
} finally {
System.out.println("Always executed");
}

🔰 10. throw Keyword


• Used to throw a custom exception.
java
CopyEdit
throw new ArithmeticException("Custom message");

After throw, no code will execute in that block.


🔰 11. throws Keyword
• Used in method declaration to inform caller about exceptions.
java
CopyEdit
public void readFile() throws IOException {
// risky code
}

Use when you don’t want to handle the exception yourself, pass it to caller.

🔰 12. Custom Exception (User-defined)


Step 1: Create a class extending Exception
Step 2: Throw that exception
java
CopyEdit
class AgeInvalidException extends Exception {
public AgeInvalidException(String msg) {
super(msg);
}
}

public class Test {


public static void main(String[] args) throws AgeInvalidException {
int age = 16;
if(age < 18) {
throw new AgeInvalidException("Underage: Not allowed");
} else {
System.out.println("Allowed");
}
}
}

🔰 13. Best Practices


✅ Always catch specific exceptions
✅ Use finally to close resources
✅ Don’t overuse throws
✅ Avoid empty catch blocks
✅ Use logging instead of System.out.println in real-world
🧪 14. Practice Questions
1. Create a program that divides two numbers and handles divide-by-zero error.
2. Write a program that catches ArrayIndexOutOfBoundsException.

3. Demonstrate use of finally block.

4. Create a custom exception called LowBalanceException.

5. Use throws to declare a method that can throw IOException.

You might also like