0% found this document useful (0 votes)
5 views

Java Exception Handling Assignment

Uploaded by

deepesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Exception Handling Assignment

Uploaded by

deepesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Exception Handling - Assignment

1. Purpose of Exception Handling in Java:

- Exception handling in Java is used to handle runtime errors, ensuring the normal flow of the

application.

- It helps to detect, handle, and recover from errors in a controlled manner.

- It prevents the abrupt termination of a program by providing mechanisms to deal with exceptional

situations.

Example:

try {

int result = 10 / 0;

} catch (ArithmeticException e) {

System.out.println("Cannot divide by zero.");

2. Difference between Checked and Unchecked Exceptions:

- **Checked Exceptions**:

- These are exceptions checked at compile time.

- The program must handle these exceptions using try-catch or by declaring them with `throws`.

- Example: IOException, SQLException.

- **Unchecked Exceptions**:

- These are exceptions not checked at compile time, but at runtime.

- The program may or may not handle these exceptions.

- Example: NullPointerException, ArithmeticException.


3. Difference between throw and throws in Exception Handling:

- **throw**:

- It is used to explicitly throw an exception.

- It is used within a method.

- Syntax: throw new ExceptionType("Error message");

- Example:

throw new ArithmeticException("Division by zero");

- **throws**:

- It is used to declare exceptions in the method signature.

- It informs the caller of the method about the exceptions that might be thrown.

- Syntax: public void methodName() throws ExceptionType.

- Example:

public void readFile() throws IOException {

FileReader file = new FileReader("file.txt");

You might also like