Exception Handling


1)A note on keywords for Error handling ?

(i) Catch - Declares the block of code used to handle an exception. (ii) Finally - Block of code , usually following a typecatch statement, which is executed no matter what program flow occurs when dealing with an exception. (iii) Throw - Used to pass an exception up to the method that calls this method. (iv) Throws - Indicates the method will pass an exception to the method that called it. (v) Try - Block of code that will be tried but which may cause an exception. (vi) Assert - Evaluates a conditional _expression to verify the programmer's assumption.

2)What are checked and unchecked exceptions ?

Checked exceptions are the ones which you expect beforehand to be raised when an exceptional condition occurs and so write your code in a try-catch block to handle that sufficiently. For example: InsuffucientBalanceException which might be raised when money is being withdrawn from a bank account and the account has insufficient balance. Checked exceptions are sub classes of Exception. Unchecked exceptions are the ones which cannot be handled in the code. These are rather unexpected exceptions like NullPointerException, OutOfMemoryError, DivideByZeroException, typically, programming errors. Unchecked exceptions are subclasses of RunTimeExceptions.

3)What is the base class from which all exceptions are subclasses ?

All exceptions are subclasses of a class called java.lang.Throwable

4) How do you intercept and thereby control exceptions ?

We can do this by using try/catch/finally blocks You place the normal processing code in try block You put the code to deal with exceptions that might arise in try block in catch block Code that must be executed no matter what happens must be place in finally block

5)How do you say that exception is handle?

When an exception is thrown in a try block and is caught by a matching catch block, the exception is considered to have been handled

6)When do we say an exception is not handled ?

There is no catch block that names either the class of exception that has been thrown or a class of exception that is a parent class of the one that has been thrown, then the exception is considered to be unhandled, in such condition the execution leaves the method directly as if no try has been executed

7) What is the difference between ‘throw’ and ‘throws’ ?And it’s application?

Exceptions that are thrown by java runtime systems can be handled by Try and catch blocks. With throw exception we can handle the exceptions thrown by the program itself. If a method is capable of causing an exception that it does not handle, it must specify this behavior so the callers of the method can guard against that exception.

8) What is the difference between ‘Exception’ and ‘error’ in java?

Exception and Error are the subclasses of the Throwable class. Exception class is used for exceptional conditions that user program should catch. With exception class we can subclass to create our own custom exception. Error defines exceptions that are not excepted to be caught by you program. Example is Stack Overflow.

9) What is ‘Resource leak’?

Freeing up other resources that might have been allocated at the beginning of a method.

10)What is the ‘finally’ block?

Finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement match the exception. Any time a method is about to return to the caller from inside try/catch block, via an uncaught exception or an explicit return statement, the finally clause is also execute.

11)In what sequence does the finally block gets executed ?

If you put finally after a try block without a matching catch block then it will be executed after the try block If it is placed after the catch block and there is no exception then also it will be executed after the try block If there is an exception and it is handled by the catch block then it will be executed after the catch block

12)What can prevent the execution of the code in finally block ?

The death of thread * Use of system.exit() * Turning off the power to CPU *

13)What are the rules for catching multiple exceptions ?

A more specific catch block must precede a more general one in the source, else it gives compilation error Only one catch block, that is first applicable one, will be executed

14) Can we have catch block with out try block? If so when?

No. Try/Catch or Try/finally form a unit.

15) What will happen to the Exception object after exception handling?

It will go for Garbage Collector. And frees the memory.

16) How many Exceptions we can define in ‘throws’ clause?

We can define multiple exceptions in throws clause. Signature is.. type method-name (parameter-list) throws exception-list

17)What does throws statement declaration in a method indicate ?

This indicates that the method throws some exception and the caller method should take care of handling it

18)Is it legal for the extending class which overrides a method which throws an exception, not o throw in the overridden class ?

Yes it is perfectly legal

19)Explain the user defined Exceptions ?

User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions. Example: class myCustomException extends Exception { // The class simply has to exist to be an exception }