Core Java Interview Questions-Exception Handling
This Core Java Interview Questions-Exception Handling page provides all the interview questions about java Exception handling including syntactical errors, logical errors, runtime errors, try block, catch block, throws keyword, throw keyword, checked exceptions, unchecked exceptions, user-defined exceptions and much more.
What is the Syntactical error?
While writing programs using any programming language, we need to follow some language rules.
Violation of these rules produce syntax errors.
What is the Logical error?
No error will be generated at the time of compilation and execution. But intended output will not be
That means the program is logically incorrect.
What is Exception?
Exception is a runtime error.
The exception occurs if we violate JVM rules in the application.
Why do we need to handle the exception?
Exception Handling is to avoid following problems
1)abnormal termination of program
2) Informal information to end user.
3) improper shutdown of resources.
What is Default exception handler?
It is a pre-defined program that handles the exception if we don’t handle in the application.
What is the super class of all the Exception classes?
java.lang.Throwable class is the super class of all the Exception classes in the hierarchy.
What are the keywords used to handle exceptions?
try, catch, throw, throws and finally.
What try block contains?
try block contains
1) Doubtful code that raises an exception.
2) Related information to Exception
What happens when exception raises in a block or method?
Block or Method creates an Exception object with the complete information of runtime error and
How to collect exception object to be handled?
By defining Exception type variables inside the catch block, we can collect exception object and handles.
When catch block executes in the application?
Only if the exception has raised in the try block.
What is the main advantage of catch block?
The catch block is used to avoid abnormal termination of the program.
Can we collect Exception object into its Parent type variable in Hierarchy?
Yes, It is the concept of Runtime binding.
Why we can’t handle errors?
An exception occurs while the application is running and these can be handled.
Errors not allowed to run the application, hence we cannot handle.
What is Unchecked Exception?
Sub class of RuntimeException class is Unchecked.
Compile will not check whether we handle the exception or not in the application.
What is Checked Exception?
Not the sub class of RuntimeException in the hierarchy.
Handling is mandatory.
The checked exception occurs when java application connected to outer resources.
Can a try block having more than 1 catch block?
Yes, and it is used to handle different types of exceptions occurs in different lines of code in the try block.
When we use try with the multi catch?
When one transaction is a collection of steps and each step there is a chance of getting an exception.
Can we handle the same exception more than one-time using single try block?
No, compiler raises an error message.
Can we handle FileNotFoundException followed by IOException?
No, once we handle the top level exception, we cannot handle its child in the same try block.
Which package contains all the Unchecked exceptions?
Error classes belong to which package?
What is finally block?
Used to place resource releasing code(closing statements) in exception handling process.
When finally block executes?
finally-block executes whether or not an exception has raised in the try block.
It executes in case of abnormal termination also.
Can we define only try-finally blocks?
Yes, it is allowed.
But in case of an exception, the program terminates abnormally after execution of finally block code.
Will finally block executes in case of abnormal termination also?
When we use “throws” keyword?
When a method is unable to handle an exception in its definition, it can pass it no the next level to be
handled using throws keyword.
Can a constructor throws Exception?
Yes, constructor also can throws an exception.
How can we define user exception?
Yes, we can.
User exception can be either Checked or Unchecked.
What is the use of “throw” keyword?
Used to throw an exception object explicitly.
we use throw keyword most used to throw user defined exception class object.
What is re-throwing of Exception?
After collecting the object in the catch block, we can re-throw the same exception for further
Can we throw pre-defined exception class object explicitly?
yes, we can create the object for pre defined exception class and it can be thrown.