Java Interview Questions - Exception

Here is a set of Java Interview Questions which focuses on Exceptions and the Exception Handling framework. You can find the previous set of Interview Questions from here:

1. What is an exception?
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
2. How does exception handling work in Java?
  1. It separates the working/functional code from the error-handling code by way of try-catch clauses.
  2. It allows a clean path for error propagation. If the called method encounters a situation it can’t manage, it can throw an exception and let the calling method deal with it.
  3. By enlisting the compiler to ensure that "exceptional" situations are anticipated and accounted for, it enforces powerful coding.
3. What are the different ways to generate an Exception?
There are two different ways to generate an Exception:
  1. Exceptions can be generated by the Java run-time system. Exceptions thrown by Java relate to fundamental errors that violate the rules of the Java language or the constraints of the Java execution environment.
  2. Exceptions can be manually generated by your code.Manually generated exceptions are typically used to report some error condition to the caller of a method.
4. What are the two types of Exception in Java?
Exceptions are of two types: Compiler-enforced exceptions, or checked exceptions and Runtime exceptions, or unchecked exceptions.
  • Compiler-enforced (checked) exceptions are instances of the Exception class or one of its subclasses — excluding the RuntimeException branch. The compiler expects all checked exceptions to be appropriately handled. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.
  • Runtime exception represent unexpected exceptional conditions which can be handled but not necessarily recovered from. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.
5. Explain the User-defined or Custom Exceptions?
User defined Exceptions are the separate Exception classes defined by the user for specific purpose. An user defined exception can be 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
}
6. Does it matter in what order catch statements for FileNotFoundException and IOException are written?
Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.
7. What is error?
An Error indicates that a non-recoverable condition has occurred that should not be caught. Error, a subclass of Throwable, is intended for drastic problems, such as OutOfMemoryError, which would be reported by the JVM itself.
8. What is the difference between exception and error?
The exception class defines mild error conditions that your program encounters. Exceptions can occur when trying to open the file which does not exist, when the network connection is disrupted, or when operands being manipulated are out of prescribed ranges.
The error class defines serious error conditions that you should not attempt to recover from. In most cases it is advisable to let the program terminate when such an error is encountered.
9. What is the super class of java.lang.Exception
java.lang.Throwable, the parent class of all exception related classes.
10. Explain the significance of try-catch blocks?
Whenever the exception occurs in Java, we need a way to tell the JVM what code to execute. To do this, we use the try and catch keywords. The try is used to define a block of code in which exceptions may occur. One or more catch clauses match a specific exception to a block of code that handles it.
Eg:
try {
   // try block for code for which we want to catch exceptions.
} catch (Exception e) {
   // catch block to handle the exception.
}
11. What is the use of finally block?
The finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. This is right place to close files, release your network sockets, connections, and perform any other cleanup your code requires.
If the try block executes with no exceptions, the finally block is executed immediately after the try block completes. It there was an exception thrown, the finally block executes immediately after the proper catch block completes.
12. What if there is a break or return statement in try block followed by finally block?
If there is a return statement in the try block, the finally block executes right after the return statement encountered, and before the return executes.
13. Can we have the try block without catch block?
Yes, we can have the try block without catch block, but finally block should follow the try block.
14. What is the difference throw and throws?
throws: Used in a method's signature if a method is capable of causing an exception that it does not handle, so that callers of the method can guard themselves against that exception. If a method is declared as throwing a particular class of exceptions, then any other method that calls it must either have a try-catch clause to handle that exception or must be declared to throw that exception (or its superclass) itself.
public void someMethod(g) throws someException {
      // Method body
}
throw: Used to trigger an exception. The exception will be caught by the nearest try-catch clause that can catch that type of exception. The flow of execution stops immediately after the throw statement; any subsequent statements are not executed.
throw new someException("Catch this one !!");

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
 

Copyright © 2012 | ScriptSplash | Powered by Blogger Templates