Advanced Java Programming - Old Questions

1. What is exception handling? Discuss the use of each keyword (try, catch, throw, throws and finally) with suitable Java program.

10 marks | Asked in 2076

An Exception is an unwanted event that interrupts the normal flow of the program. When an exception occurs program execution gets terminated. In such cases we get a system generated error message. 

Exception handling is a powerful mechanism or technique that allows us to handle runtime errors in a program so that the normal flow of the program can be maintained. Java exception handling is important because it helps maintain the normal, desired flow of the program even when unexpected events occur. If Java exceptions are not handled, programs may crash or requests may fail.  Suppose there are 10 statements in a program and an exception occurs at statement 5; the rest of the code will not be executed, i.e., statements 6 to 10 will not be executed. However, when we perform exception handling, the rest of the statements will be executed. That is why we use exception handling

Java provides five keywords that are used to handle the exception. 

1. try:  The "try" keyword is used to specify a block where an exception can occur. It is always followed by a catch block, which handles the exception that occurs in the associated try block. A try block must be followed by catch blocks or finally block or both.

2. Catch: The "catch" block is used to handle the exception. This block must follow the try block and a single try block can have several catch blocks associated with it. When an exception occurs in a try block, the corresponding catch block that handles that particular exception executes. It can be followed by finally block later.

3. Finally: A finally block contains all the crucial statements that must be executed whether an exception occurs or not. The statements present in this block will always execute, regardless an exception occurs in the try block or not such as closing a connection, stream etc.

4. throw: The "throw" keyword is used to throw an exception.

5. throws: The "throws" keyword is used to declare exceptions. It specifies that there may occur an exception in the method. It doesn't throw an exception. It is always used with method signature.


class TestExceptions {
   static void myMethod(int testnum) throws Exception {
      System.out.println ("start - myMethod");
      if (testnum == 12) 
         throw new Exception();
      System.out.println("end - myMethod");
   public static void main(String  args[]) {
      int testnum = 12;
      try {
         System.out.println("try - first statement");
         System.out.println("try - last statement");
      catch ( Exception ex) {
         System.out.println("An Exception");
      finally {
         System. out. println( "finally") ;
      System.out.println("Out of try/catch/finally - statement");
try - first statement
start - myMethod
An Exception
Out of try/catch/finally - statement