Saturday, February 15, 2014

Exception Handling Interview Questions

1.What are the 3 types of error?

1. Compile Time Error
2. Runtime Error
3. Error

2.What is Throwable?

Throwable is a class, for all errors and Exceptions.

3.What is the super class for all exceptions?

Exception class.

4.How will you create own exception in java?

You have to extends Exception class to create your own exception.

5.What are checked exception or Compile time Error?

The java compiler will check the exception at compile time is called checked exception.

6.What is unchecked exception or Runtime exception?

JVM will check the exception at runtime called unchecked exception.

7.What is Error?

Error is an error and it can not be handled by programmer. For example Memory error.

8.What is try catch in java?

try is the block , you can write your code logic here. Suppose if any exception will come in try block, you have to catch it.
For that you will write catch block to catch the exception.

For example,
try {
// DB connection coding
   }
catch(SQLException e){
e.printStackTrace();
}

9.What is throws and throw ?

If the programmer dont want to handle the exception, at the same time, programmer want to throw exception out of a method, throws will be helpful.

If the programmer want to throw the exception explicitly , then should handle it in catch block. 

So please remember throws and throw both are very different.

10.What is finally block?

whether the exception will occur or not, finally block will be executed. 

For example,

try {
// DB connection coding
   }
catch(SQLException e){
e.printStackTrace();
}
finally {
        // DB connection close code
}

So here, the exception will occur or not, finally bloack will be executed. DB connection will also close for security purpose.

11.Can I write the try block without catch block?

Yes. you can write try block without catch block.

12.Can I write the multiple catch block?

Yes.

13.What is nested try?

If you will write a try block within aother try, is called nested try.

14.Can I write the code between try and catch?

No.

No comments:

Post a Comment