Exception Handling
Interview Questions
1) What is an exception?
An exception is an abnormal condition that occurs
during the execution of a program and disrupts the normal flow of the program. This
exception must be handled properly. If it is not handled, the program will be
terminated abruptly.
2) How the exceptions are handled in java? OR Explain exception handling mechanism in java?
Exceptions in java are handled using try, catch and finally blocks.
try block: The code or set of statements that are to be monitored for exceptions are kept in this block.
catch block: This block catches the exceptions occurred in the try block.
finally block: This block is always executed whether exception is occurred in the try block or not and occurred exception is caught in the catch block or not.
3) What is the difference between error and exception in java?
Errors are mainly caused by the environment in which an application is running. For example, OutOfMemoryError happens when JVM runs out of memory. Whereas exceptions are mainly caused by the application itself. For example, NullPointerException occurs when an application tries to access the null object.
4) Can we keep other statements in between try, catch and finally blocks?
No. We shouldn’t write any other statements in between try, catch, and finally blocks. They form one unit.
try
{
// Statements to be monitored for exceptions
}
//You can't keep statements here
catch(Exception ex)
{
//Cathcing the exceptions here
}
//You can't keep statements here
finally
{
// This block is always executed
}
2) How the exceptions are handled in java? OR Explain exception handling mechanism in java?
Exceptions in java are handled using try, catch and finally blocks.
try block: The code or set of statements that are to be monitored for exceptions are kept in this block.
catch block: This block catches the exceptions occurred in the try block.
finally block: This block is always executed whether exception is occurred in the try block or not and occurred exception is caught in the catch block or not.
3) What is the difference between error and exception in java?
Errors are mainly caused by the environment in which an application is running. For example, OutOfMemoryError happens when JVM runs out of memory. Whereas exceptions are mainly caused by the application itself. For example, NullPointerException occurs when an application tries to access the null object.
4) Can we keep other statements in between try, catch and finally blocks?
No. We shouldn’t write any other statements in between try, catch, and finally blocks. They form one unit.
try
{
// Statements to be monitored for exceptions
}
//You can't keep statements here
catch(Exception ex)
{
//Cathcing the exceptions here
}
//You can't keep statements here
finally
{
// This block is always executed
}
5) Can we write only try block without catch and finally blocks?
No, It shows a compilation error. The try block must be followed by either catch or finally block. You can remove either catch block or finally block but not both.
6) There are three statements in a try block – statement1, statement2 and statement3. After that, there is a catch block to catch the exceptions that occurred in the try block. Assume that exception has occurred in statement2. Does statement3 get executed or not?
No. Once a try block throws an exception, the remaining statements will not be executed. control comes directly to catch block.
7) What is unreachable catch block error?
When you are keeping multiple catch blocks, the order of catch blocks must be from most specific to most general ones. i.e subclasses of Exception must come first and superclasses later. If you keep superclasses first and subclasses later, the compiler will show unreachable catch block error.
public class ExceptionHandling
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt("abc"); //This statement throws NumberFormatException
}
catch(Exception ex)
{
System.out.println("This block handles all exception types");
}
catch(NumberFormatException ex)
{
//Compile time error
//This block becomes unreachable as
//exception is already caught by the above catch block
}
}
}
8) What are run time exceptions in java. Give example?
The exceptions which occur at run-time are called run time exceptions. These exceptions are unknown to the compiler. All sub classes of java.lang.RunTimeException and java.lang.Error are run time exceptions. These exceptions are unchecked type of exceptions. For example, NumberFormatException, NullPointerException, ClassCastException, ArrayIndexOutOfBoundException, StackOverflowError etc.
9) What is OutOfMemoryError in java?
OutOfMemoryError is the subclass of java.lang.Error occurs when JVM runs out of memory.
10) what are checked and unchecked exceptions in java?
Checked exceptions are the exceptions that are known to the compiler. These exceptions are checked at compile-time only. Hence the name-checked exceptions. These exceptions are also called compile-time exceptions. Because these exceptions will be known during compile time.
Unchecked exceptions are those exceptions that are not at all known to the compiler. These exceptions occur only at run time. These exceptions are also called run time exceptions. All subclasses of java.lang.RunTimeException and java.lang.Error are unchecked exceptions.
11) Can we keep the statements after finally block If the control is returning from the finally block itself?
No, it gives unreachable code error. Because control is returning from the finally block itself. The compiler will not see the statements after it. That’s why it shows an unreachable code error.
12) Does finally block get executed If either try or catch blocks are returning the control?
Yes, finally block will be always executed no matter whether try or catch blocks are returning the control or not.
13) Can we throw an exception manually? If yes, how?
Yes, we can throw an exception manually using the throw keyword. Syntax for throwing an exception manually is
throw InstanceOfThrowableType;
The below example shows how to use throw keyword to throw an exception manually.
try
{
NumberFormatException ex = new NumberFormatException(); //Creating an object to NumberFormatException explicitly
throw ex; //throwing NumberFormatException object explicitly using throw keyword
}
catch(NumberFormatException ex)
{
System.out.println("explicitly thrown NumberFormatException object will be caught here");
}
14) What is Re-throwing an exception in java?
Exceptions raised in the try block are handled in the catch block. If it is unable to handle that exception, it can re-throw that exception using throw keyword. It is called re-throwing an exception.
try
{
String s = null;
System.out.println(s.length()); //This statement throws NullPointerException
}
catch(NullPointerException ex)
{
System.out.println("NullPointerException is caught here");
throw ex; //Re-throwing NullPointerException
}
15) Why it is always recommended that clean up operations like closing the DB resources to keep inside a finally block?
Because finally block is always executed whether exceptions are raised in the try block or not and raised exceptions are caught in the catch block or not. By keeping the cleanup operations in finally block, you will ensure that those operations will be always executed irrespective of whether exception has occurred or not.
16) What is ClassCastException in java?
ClassCastException is a RunTimeException which occurs when JVM unable to cast an object of one type to another type.
17) What is StackOverflowError in java?
StackOverflowError is an error that is thrown by the JVM when stack overflows.
18) Can we override a superclass method that is throwing an unchecked exception with the checked exception in the sub-class?
No. If a superclass method is throwing an unchecked exception, then it can be overridden in the subclass with the same exception or any other unchecked exceptions but can not be overridden with checked exceptions.
19) Which class is the superclass for all types of errors and exceptions in java?
java.lang.Throwable is the superclass for all types of errors and exceptions in java.
20) What are the legal combinations of try, catch and finally blocks?
1)
try
{
//try block
}
catch(Exception ex)
{
//catch block
}
2)
try
{
//try block
}
finally
{
//finally block
}
3)
try
{
//try block
}
catch(Exception ex)
{
//catch block
}
finally
{
//finally block
}
21) What is the use of the printStackTrace() method?
printStackTrace() method is used to print the detailed information about the exception that occurred.
22) Give some examples to checked exceptions?
ClassNotFoundException, SQLException, IOException
23) Give some examples of unchecked exceptions?
NullPointerException, ArrayIndexOutOfBoundsException, NumberFormatException
No comments:
Post a Comment