Exceptions Interview Questions

Exceptions Interview Questions

  1. What is an exception?
    The English literature meaning is "something that happens unusually (very rarely)". It is the same meaning with Java also. Exception can be defined as a problem occurring at runtime that prevents the execution of the program. Or to say, an exception is a runtime problem (not compile time as compilation is already over) that terminates the program execution abruptly.
  2. What are the causes of exceptions to occur?
    It is simply due to the end-user (actual user of the software). It is due to the wrong interaction with a running system feeding wrong data which the program execution does not understand what to do or cannot give any output depending on the data user feeds. For example, entering zero for a denominator value or entering a wrong IP address in a network to connect etc.
  3. What is exception handling or why should we handle the exception?
    When an exception is thrown by the JVM, the execution simply terminates without executing the expected whole program. To continue the execution, even if an exception occurs, exception handling is required. Handling the exception thrown the by JVM so that the program’s execution goes further is known as "exception handling".
  4. What are try and catch blocks?
    "try" and "catch" are keywords of Java used with exception handling mechanism only. When the code is being developed, the programmer should place the trouble making statements (generally the statements that ask the user to enter some data) in a try block with a suitable exception handler in catch block.
  5. What is an exception handler?
    Exception handler is a Java class capable to handle the exception thrown by the system (JVM). Exception handler is placed in the catch statement. It is very important to place the correct handler that can catch the exception successfully. When an exception is thrown by the try block, the catch block catches the exception and if the try block does not throw any exception (due to correct feed of data), the catch block execution is simply ignored (that is, does not execute at all like else will not be executed when if is executed).
  6. What is finally block?
    “finally” is a keyword of Java used with exception handling only. If the programmer places a wrong handler with catch statement, the exception handling mechanism does not work and thereby the execution terminates without executing all the remaining statements. In the remaining statements, a few may be very important that must be executed before the program gets terminated. Place these important statements in "finally" block. That is, finally block statements are guaranteed of execution even if the programmer does not handle the exception successfully. Having finally block is optional. Generally, finally block comes with combination of try-catch.
  7. Mention some important statements that require fianlly block attention?
    The statements include like closing statements of IO stream and socket hanldes, database connections etc. (known as cleanup operations that prevents memory leakage).
  8. What is throws keyword?
    "throws" keyword can be used in two ways in Java.
    1. Used to inform the programmer about the possible problems that may occur while using the method known as "claiming the exception".
    2. As an alternative to try-catch block.
  9. What is throw keyword?
    Using throw keyword, the programmer can throw the exception to the system (JVM) explicitly.
  10. What is the difference between "throws" and "throw"?
    Both look alike but they differ a lot in their usage. throws is used to claim the exception and throw is used to throw the exception explicitly.
  11. How many ways an exception can be handled in Java?
    There are three ways to handle.
    1. With try-catch block.
    2. With throws.
    3. With throw.
  12. Can a catch exist without try or finally exist without catch?
    catch should come in combination with try block like else cannot exist wihout if. catch block can be replaced with finally block. That is, with try there can come either catch or finally. They best combination, generally used is, try-catch-finally.
  13. What is the super class of all exceptions of Java?
    It is class Throwable from java.lang package.
  14. What is Error exception in Java?
    It is an unchecked exception and a subclass of Throwable. It is generally used for handling the exceptions thrown by the JVM at runtime like memory insufficient etc.
  15. What is the rule to be observed regarding exceptions in method overriding?
    The overridden method in subclass cannot throw more exceptions than that of super class method. There is a rule to be followed with access specifiers also in method overriding.
  16. What is NumberFormatException?
    It is thrown by the parsing methods when the method is unable to parse (convert) the string data into primitive data types.

Pass your comments on this tutorial "Exceptions Interview Questions" to improve the quality.

4 thoughts on “Exceptions Interview Questions

  1. Shalini

    Respected Sir,
    Exception class is the super class of all kinds of exception.How can it handle all kind of exception as a parent class object doesn’t have the access to child class method.But how does Exception class object does it.
    Also Sir when we can handle all kind of exception from one Exception class .So what is the advantage of handling different kinds of exception with its sub classes?

    1. S. Nageswara Rao, Corporate Trainer Post author

      We know a subclass object can be assigned to superclass object implicitly. When, for example, ArithmeticException is thrown by JVM when encountered division by zero, if in the catch block Exception exists, the ArithmeticException is assigned to an object of RuntimeException and as RuntimeException does not exist (as catch contains Exception), the RuntimeException is assigned to Exception. All these conversions makes your program to run slower. That is why, place exact exception class in the catch block.

  2. Sudhakar Reddy

    How many ways an exception can be handled in Java?
    There are three ways to handle.
    With try-catch block.
    With throws.
    With throw.
    Please justify with example for the option 3 ?
    As of my knowledge throw is used raise an exception, but how can you handle the exceptions.

    – See more at: http://way2java.com/java-questions/exception-
    handling/#sthash.wCf5vcEv.dpuf

Leave a Reply

Your email address will not be published. Required fields are marked *