Wednesday, January 25, 2012

Core Java Interview Questions - Exception Handling

The following are some questions you might encounter with respect to Java Exception Handling in any Interview. Exception Handling is an important part of any Java Application because, this determines how gracefully you capture and handle unexpected situations. 

Apart from the questions below, there are a few articles that I have put up (as part of the SCJP Certification series) on Exception Handling that you might find useful. You can use them to revise/review your understanding of Exception Handling. 

They are: 

Exception Handling 
Common Exceptions & Errors 


1. How could Java classes direct messages to a file instead of the Console? 

The System class has a variable "out" that represents the standard output, and the variable "err" that represents the standard error device. By default, they both point at the system console. 

The standard output could be re-directed to a file as follows: 

Stream st = new Stream(new FileOutputStream("output.txt")); 

2. Does it matter in what order catch statements for FileNotFoundException and IOException are written? 

Yes, it does. The child exceptions classes must always be caught first and the "Exception" class should be caught last. 

3. What is user-defined exception in java ? 

User-defined expections are the exceptions defined by the application developer which are errors related to specific application. Application Developer can define the user defined exception by inheriting the Exception class. Using this class we can create & throw new exceptions. 

4. What is the difference between checked and Unchecked Exceptions in Java ? 

Checked exceptions must be caught using try-catch() block or thrown using throws clause. If you dont, compilation of program will fail. whereas we need not catch or throw Unchecked exceptions. 

5. What is the catch or declare rule for method declarations? 

If a checked exception may be thrown within the body of a method, the method must either catch that exception or declare it in its throws clause. This is done to ensure that there are no orphan exceptions that are not handled by any method. 

6. What is the purpose of the finally clause of a try-catch-finally statement? 

The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown or caught. It is usually used in places where we are connecting to a database so that, we can close the connection or perform any cleanup even if the query execution in the try block caused an exception. 

7. What classes of exceptions may be caught by a catch clause? 

A catch clause can catch any exception that may be assigned to the Throwable type. This includes the Error and Exception types. 

8. Can an exception be rethrown? 

Yes, an exception can be rethrown any number of times. 

9. When is the finally clause of a try-catch-finally statement executed? 

The finally clause of the try-catch-finally statement is always executed after the catch block is executed, unless the thread of execution terminates or an exception occurs within the execution of the finally clause. 

10. What classes of exceptions may be thrown by a throw statement? 

A throw statement may throw any expression that may be assigned to the Throwable type. 

11. What happens if an exception is not caught? 

An uncaught exception results in the uncaughtException() method of the thread's ThreadGroup being invoked, which eventually results in the termination of the program in which it is thrown. 

12. What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement?

The exception propagates up to the next higher level try-catch statement (if any) or results in the program's termination. 

13. Can try statements be nested? 

Try statements can be tested. It is possible to nest them to any level, but it is preferable to keep the nesting to 2 or 3 levels at max. 

14. How does a try statement determine which catch clause should be used to handle an exception?

When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception that was thrown, is executed. The remaining catch clauses are ignored. 

15. What is difference between error and exception 

Error occurs at runtime and cannot be recovered, Outofmemory is one such example. Exceptions on the other hand are due conditions which the application encounters, that can be recovered such as FileNotFound exception or IO exceptions 

16. What is the base class from which all exceptions are subclasses 

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

17. How do you intercept and control exceptions 

We can intercept and control exceptions 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 

18. When do we say an exception is handled 

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. Or when an exception thrown by a method is caught by the calling method and handled, an exception can be considered handled. 

19. 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 

20. 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 

21. What can prevent the execution of the code in finally block 

Theoretically, the finally block will execute no matter what. But practically, the following scenarios can prevent the execution of the finally block. 

* The death of thread 
* Use of system.exit() 
* Turning off the power to CPU 
* An exception arising in the finally block itself 

22. 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 about unreachable code blocks. 

23. 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. If a method invokes another method that throws some exception, the compiler will complain until the method itself throws it or surrounds the method invocation with a try-catch block. 

24. What are checked exceptions? 

Checked exceptions are exceptions that arise in a correct program, typically due to user mistakes like entering wrong data or I/O problems. Checked Exceptions can be caught and handled by the programmer to avoid random error messages on screen. 

25. What are runtime exceptions 

Runtime exceptions are due to programming bugs like out of bound arrays or null pointer exceptions. 

26. What is difference between Exception and errors 

Errors are situations that cannot be recovered and the system will just crash or end. Whereas, Exceptions are just unexpected situations that can be handled and the system can recover from it. We usually catch & handle exceptions while we dont handle Errors. 

27. How will you handle the checked exceptions 

You can provide a try/catch block to handle it or throw the exception from the method and have the calling method handle it. 

28. When you extend a class and override a method, can this new method throw exceptions other than those that were declared by the original method? 

No it cannot throw, except for the subclasses of the exceptions thrown by the parent class's method. 

29. Is it legal for the extending class which overrides a method which throws an exception, not to throw in the overridden class? 

Yes, it is perfectly legal.

If you have any questions that you want answer for, please leave a comment on this page OR drop a note to Snehal[at]TechProceed[dot]com and I will answer them.

Happy Learning!

No comments :

Post a Comment