EXCEPTION HANDLING Flashcards

1
Q

What is exception handling?

A

Java Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an exception?

A

Exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program’s instructions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between an error and an exception

A

Error: An Error indicates a serious problem that a reasonable application should not try to catch.
Exception: Exception indicates conditions that a reasonable application might try to catch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe and illustrate the exception hierarchy

A

All exception and error types are subclasses of class Throwable, which is the base class of the hierarchy. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception. Another branch, Error is used by the Java run-time system(JVM) to indicate errors having to do with the run-time environment itself(JRE). StackOverflowError is an example of such an error.

*See https://www.geeksforgeeks.org/exceptions-in-java/#:~:text=Java%20Exception%20Handling%20is%20a,flow%20of%20the%20program’s%20instructions. for pic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different types of exceptions

A
  1. Built-in Exceptions
    - Checked Exception- Checked exceptions are called compile-time exceptions because these exceptions are checked at compile-time by the compiler. ClassNotFoundException, IOException, SQLException etc are the examples of the checked exceptions.
    - Unchecked Exception- The unchecked exceptions are just opposite to the checked exceptions. The compiler will not check these exceptions at compile time. In simple words, if a program throws an unchecked exception, and even if we didn’t handle or declare it, the program would not give a compilation error. Some common unchecked exceptions in Java are NullPointerException, ArrayIndexOutOfBoundsException and IllegalArgumentException.

2.User-Defined Exceptions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the syntax of the try catch block

A
try {
   block of code that can throw exceptions
} catch (Exception ex) {
   Exception handler
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the syntax of the try catch finally block

A
try {
  block of code that can throw exceptions
} catch (ExceptionType1 ex1) {
   exception handler for ExceptionType1
} catch (ExceptionType2 ex2) {
   Exception handler for ExceptionType2
} finally {
 finally block always executes
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Give an example of how to use throw keyword

A

throw new Exception(“Exception message”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give an example of how to use throws keyword

A
void testMethod() throws ArithmeticException, ArrayIndexOutOfBoundsException {
    rest of code
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly