In this post there is a collection of Java exception handling interview questions and answers which’ll be helpful for interview preparation. Java Exception Handling Interview Questions 1.What is Exception?
An exception can be defined as a condition that disrupts the normal flow of your code. Since exception happens at run time the current code processing can not continue, it needs to handle that exception that’s where the exception handler mechanism takes over. Read more about Exception here. 2.What is error?
Error and its subclasses define exceptions that your program cannot handle, like out of memory error. As example – StackOverflowError, OutOfMemoryError 3.How does exception handling works in Java?
When an exceptional condition happens in any method the procedure for exception handling in Java is as follows•An exception object which encapsulates the information about the error, type of the exception, where exception happened is created. •The execution of the code in the method is stopped and created exception is thrown. •The exception handling mechanism looks for the exception handler that can handle the thrown exception. The method where exception is thrown may handle that exception itself or leave it to the calling method to handle it. •If no exception handler is provided for the thrown exception, default exception handler will be called to handle that exception. Default exception handler prints the stack trace from the point exception was thrown and terminates the program. Read more about Exception here. 4.How is exception handling managed in Java?
Exception handling in Java is managed using 5 keywords- try, catch, finally, throw and throws 5.Which is the parent class of all the exception classes in Java?
Throwable class is the parent class of all the exception classes in Java. 6.Explain exception hierarchy in Java?
Throwable class is the parent class of all the exception classes in Java. Just below Throwable in Java exception hierarchy are two subclasses that divide exception into two