Q. What is exception handling?
A: An Exception handling is nothing, it is a mechanism to handle exception and exception handling is done with help of Try-catch and finally block.Q. What is an exception?
A. An Exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions.
Q. what is the differences between exception and error classes?
A. Exception can be handled where as error can not be handled.
Q. Why an exception occurs?
A: There can be several reasons that can cause a program to throw exception. For example: Opening a non-existing file in your program, Network connection problem, bad input data provided by user etc.
Q. How many types of exceptions are there?.
A. There are two types of exceptions:
- Checked exception
- Unchecked exception
Unchecked exception: These exceptions are mandatory to handle. If a methods needs to propagate this exception to its calling method using throw keyword.
Checked exceptions
All exceptions other than Runtime Exceptions are known as Checked exceptions as the compiler checks them during compilation to see whether the programmer has handled them or not. If these exceptions are not handled/declared in the program, you will get compilation error. For example, SQLException, IOException, ClassNotFoundException etc.
Unchecked Exceptions
Runtime Exceptions are also known as Unchecked Exceptions. These exceptions are not checked at compile-time so compiler does not check whether the programmer has handled them or not but it’s the responsibility of the programmer to handle these exceptions and provide a safe exit. For example, ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Q. what are the difference between Checked exception and Unchecked exception in java?A: There are many differences:
1. in Checked exception, try-catch block is mandatory to handle exception where as in Unchecked exception, try-catch block is optional to handle exception.
--------------------------------------------------------------
Points remember:1. In order to handle multiple exceptions in single catch, you can specify multiple exception classes in one catch block by separating them by Pipe(|) character.
Note: catch block of child exception must come first in the order
2. when unchecked exception is propagated from services classes then transaction will be roll backed.
Q. what is the differences between throw and throws keywords?
A. There are many differences:
- Throw keyword raise the exception whereas Throws keyword propagate the exceptions.
- Throw keyword is used inside a function. It is used when it is required to throw an Exception logically where as Throws keyword is in the function signature. It is used when the function has some statements that can lead to some exceptions.
- Throw keyword is used to throw an exception explicitly. It can throw only one exception at a time where as Throws keyword can be used to declare multiple exceptions, separated by comma. Whichever exception occurs, if matched with the declared ones, is thrown automatically.
- Checked exception cannot be propagated using throw only.Unchecked exception can be propagated using throw where as For the propagation checked exception must use throws keyword followed by specific exception class name.
- Checked exceptions are propagated by throw keyword where as Unchecked exceptions are automatically propagated.
No comments:
Post a Comment