Top 40+ Java  Exception Handling Interview Questions and Answers - 1

Question: 1

What is difference between throw and throws keyword in Java?

throws keyword is used with method signature to declare the exceptions that the method might throw whereas throw keyword is used to disrupts the flow of program and handing over the exception object to runtime to handle it.

Question: 2

When is finally block NOT called?

Finally block is NOT called in following cases: if the JVM exists while the try or catch code is being executed. This may happen due to System.exit() call.

If the thread executing the try or catch code gets interrupted or killed. In this case, the finally block may not execute although the application keeps working.

If an expression is thrown in finally block is not handled. In this case, the remaining code is finally block may not execute.

Question: 3

What is the Array Store Exception thrown?

When copying elements between different arrays, if the sources or destination arguments are not arrays or their types are not compatible, an Array Store Exception will be thrown.

Question: 4

What situations are best suitable for implementing assertions?

Assertions can best be implemented.

As Internal Invariants

As Control flow Invariants

As Pre conditions and Post conditions

As Class Invariants

Question: 5

Can we have an empty catch block?

We can have an empty catch block but it’s the example of worst programming.

We should never have empty catch block because if the exception is caught by that block, we will have no information about the exception and it will be a nightmare to debug it.

There should be at least a logging statement to log the exception details in console or log files.

Error Report!

Related Questions