Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Exceptions in Java

Exceptions in Java are events that disrupt program execution, typically due to runtime errors. They are categorized into checked exceptions, which must be handled at compile time, and unchecked exceptions, which are handled at runtime. Java provides mechanisms like try-catch-finally blocks for exception handling, and developers can create custom exceptions by extending existing exception classes.

Uploaded by

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

Exceptions in Java

Exceptions in Java are events that disrupt program execution, typically due to runtime errors. They are categorized into checked exceptions, which must be handled at compile time, and unchecked exceptions, which are handled at runtime. Java provides mechanisms like try-catch-finally blocks for exception handling, and developers can create custom exceptions by extending existing exception classes.

Uploaded by

SachinJadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

🚨 Exceptions in Java

Exceptions in Java are events that disrupt the normal flow of a program's
execution. They usually occur due to runtime errors like dividing by zero,
accessing an invalid index, or opening a file that doesn't exist.

✅ Key Concepts
🔹 What is an Exception?

An exception is an object that represents an error or unexpected


condition. It is thrown by the Java Virtual Machine (JVM) or manually by
your code.

⚙️Types of Exceptions
1. Checked Exceptions

Handled at compile time. If not handled, the program will not compile.

Examples:

• IOException

• SQLException

java
CopyEdit
import java.io.*;

public class FileReadExample {


public static void main(String[] args) throws
IOException {
FileReader file = new FileReader("test.txt");
BufferedReader fileInput = new
BufferedReader(file);
System.out.println(fileInput.readLine());
fileInput.close();
}
}
2. Unchecked Exceptions

Handled at runtime. These are subclasses of RuntimeException.

Examples:
• ArithmeticException

• NullPointerException

• ArrayIndexOutOfBoundsException

java
CopyEdit
public class DivideExample {
public static void main(String[] args) {
int a = 10, b = 0;
int c = a / b; // ArithmeticException: / by zero
System.out.println(c);
}
}

📦 Exception Hierarchy
php
CopyEdit
Throwable
├── Error (serious problems, not caught by programs)
└── Exception
├── Checked (e.g., IOException)
└── Unchecked (RuntimeException)

🔐 Exception Handling in Java


Java uses try-catch-finally blocks to handle exceptions.

🔸 try-catch Example

java
CopyEdit
public class TryCatchExample {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
}
}
}
🔸 finally Block

The finally block always runs, whether an exception is thrown or not.


java
CopyEdit
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Error occurred");
} finally {
System.out.println("This block always runs");
}

🚀 Throw and Throws


🔹 throw

Used to explicitly throw an exception.

java
CopyEdit
public class ThrowExample {
public static void main(String[] args) {
throw new ArithmeticException("Manually thrown
exception");
}
}
🔹 throws

Declares exceptions that a method might throw.

java
CopyEdit
public void readFile() throws IOException {
FileReader file = new FileReader("data.txt");
}

Custom Exceptions
You can create your own exception class by extending Exception or
RuntimeException.

java
CopyEdit
class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
public class CustomExample {
public static void main(String[] args) {
try {
throw new MyException("Custom exception
thrown");
} catch (MyException e) {
System.out.println(e.getMessage());
}
}
}

✅ Best Practices
• Catch specific exceptions instead of generic ones.

• Use finally to release resources (e.g., close files or DB connections).

• Avoid overusing throws for unchecked exceptions.

• Log exceptions for debugging.

You might also like