Wednesday 29 February 2012

Java Execption Handling

1) What is Exception hierarchy?
At the top there is Throwable class .It has two Su classes Exception and Error.

Exception: It is defined as some unwanted condition which abnormally terminates the program flow. Recovery from exceptions occurred is possible using java exception handling

Error: It is a condition from which recovery is not possible.e.g. OutOFMemor Error



2) What are different types of Exception

Three are basically two types of Exceptions

1) Checked Exception: Checked Exceptions are those which compiler forces to user to handle or declare. It excludes RuntimeExceptions and its subclasses

2)Unchecked Exceptions :All exceptions extending RunTimeException are unchecked .As Java programmers cannot judge at compile time whether these Runtime exceptions will occur or not therefore Runtime Exceptions are unchecked Also Error and its subclasses are unchecked. As recovery from Error is not possible Error is unchecked

3) How the exception is handled in java

1) Using try-catch-finally

2) Using throws clause with method

3) Using throw keyword

4) What are rule about try-catch-finally

1) Every try must have either catch or finally

2) try-catch-finally must be in sequence i.e first try then catch then finally

3) There should not be any piece of code between try block and catch block or finally block

4)If there are multiple catch blocks having parent child relationship of Exception then first subclass exception must be caught then the super class exception is to be caught

5) What is finally block

Finally block is guaranteed to be executed .All clean up code goes into finally block.e.g.code for closing database connection

6)If try block contains return or break statement as last line whether the finally block executed

Yes. Return or break statement will execute of finally block



7) If try block contains System. exit statement as last line whether the finally block executed

No.JVM will terminate no further execution after System.exit

8) How custom Exceptions are created

By extending the Exception class

9) What is difference between throw and throws

Throw is used to throw exception at any line in java code

Throws is use with method to declare that this method doesn’t handle the exception its delegating task of exception handling to calling method

10)What is difference between NoClassDefFoundError Vs ClassNotFoundException


ClassNotFoundException is exception.It occurs when class is not found in classpath


NoClassDefFoundError is an error.It does not mena that the required class not found in classpath. Class was found but it may contain some static block which tries to load some another class whose definition jvm is not able to load at runtime




No comments:

Post a Comment