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

Java Exception Handling Assignment

Uploaded by

deepesh
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 views2 pages

Java Exception Handling Assignment

Uploaded by

deepesh
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/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy