What is exception handling?
Java Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.
What is an exception?
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.
What is the difference between an error and an exception
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.
Describe and illustrate the exception hierarchy
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
What are the different types of exceptions
2.User-Defined Exceptions
What is the syntax of the try catch block
try {
block of code that can throw exceptions
} catch (Exception ex) {
Exception handler
}What is the syntax of the try catch finally block
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
}Give an example of how to use throw keyword
throw new Exception(“Exception message”);
Give an example of how to use throws keyword
void testMethod() throws ArithmeticException, ArrayIndexOutOfBoundsException {
rest of code
}