Exception Handling and their uses
By Ramakrishna on Apr 6, 2009 in Java Interview Questions
Q) What is the difference between ‘throw’ and ‘throws’ ?And it’s application?
Rep) 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.
Q) What is the difference between ‘Exception’ and ‘error’ in java?
Rep) 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.
Q) What is ‘Resource leak’?
Rep) Freeing up other resources that might have been allocated at the beginning of a method.
Q) What is the ‘finally’ block?
Rep) 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.
Q) Can we have catch block with out try block? If so when?
Rep) No. Try/Catch or Try/finally form a unit.
Q) What will happen to the Exception object after exception handling?
Rep) It will go for Garbage Collector. And frees the memory.
Q) How many Exceptions we can define in ‘throws’ clause?
Rep) We can define multiple exceptions in throws clause.
Signature is.. : type method-name (parameter-list) throws exception-list
Q) The finally block is executed when an exception is thrown, even if no catch matches it.
True/False
Rep) True
Q) The subclass exception should precede the base class exception when used within the catch clause.
True/False
Rep) True
Q) Exceptions can be caught or rethrown to a calling method.
True/False
Rep) True
Q) The statements following the throw keyword in a program are not executed.
True/False
Rep) True
Q) The toString ( ) method in the user-defined exception class is overridden.
True/False
Rep) True

2 Comment(s)
By chitra on Jul 16, 2009 | Reply
hai
ya hi
By java interview on Sep 22, 2009 | Reply
this one is very useful, exception is the must-learn in java