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

00ps 4

The document explains the differences between checked and unchecked exceptions in Java, detailing how checked exceptions are verified at compile-time while unchecked exceptions occur at runtime. It describes the functionality of try-catch blocks for handling exceptions, the purpose of the finally block for cleanup, and the usage of throw and throws keywords in exception handling. Additionally, it provides guidance on creating and using custom exceptions in Java with an example.

Uploaded by

Darshit
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)
5 views2 pages

00ps 4

The document explains the differences between checked and unchecked exceptions in Java, detailing how checked exceptions are verified at compile-time while unchecked exceptions occur at runtime. It describes the functionality of try-catch blocks for handling exceptions, the purpose of the finally block for cleanup, and the usage of throw and throws keywords in exception handling. Additionally, it provides guidance on creating and using custom exceptions in Java with an example.

Uploaded by

Darshit
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

00ps 4

Reflection Question Answers

---

1. What is the difference between checked and unchecked exceptions in Java?

Checked Exceptions: These are exceptions checked at compile-time. The compiler ensures
these exceptions are either caught in a try-catch block or declared using the throws keyword.
Examples: IOException, SQLException.

Unchecked Exceptions: These are runtime exceptions that are not checked at compile-time.
They typically occur due to programming errors like accessing an invalid index. Examples:
NullPointerException, ArithmeticException.

---

2. How does the try-catch block work in exception handling?

The try block contains code that might throw an exception.

If an exception occurs, the program jumps to the catch block, where the exception is handled.

If no exception occurs, the catch block is skipped, and the program continues executing the
code after the try-catch block.

---

3. What is the purpose of the finally block in Java exception handling?

The finally block contains code that is always executed, regardless of whether an exception
occurs.

It is typically used for cleanup operations such as closing files, releasing resources, or resetting
states.

Even if the try block contains a return statement, the finally block will still execute.

---

4. How do you use the throw and throws keywords in Java?


throw: Used to explicitly throw an exception from within a method or a block of code. Example:
throw new ArithmeticException("Error message");.

throws: Used in a method signature to declare that the method might throw certain exceptions.
The caller must handle or propagate these exceptions. Example: void readFile() throws
IOException.

---

5. How can you create and use custom exceptions in Java?

To create a custom exception, extend the Exception class or its subclass.

Define a constructor to initialize the exception message.

Use throw to raise the custom exception when specific conditions are met.

Handle the custom exception in the catch block.

Example:

class CustomException extends Exception {


public CustomException(String message) {
super(message);
}
}

public class Example {


public static void main(String[] args) {
try {
throw new CustomException("This is a custom exception");
} catch (CustomException e) {
System.out.println(e.getMessage());
}
}
}

---

Let me know if you need further clarifications!

You might also like