Sunday, 25 June 2017

1. Introduction to Exception handling

1. What is an Exception?
  1. An exception is an abnormal event that arises during the execution of the program and disrupts the normal flow of the program.
  2. Exception will arise when user enters invalid input, loss of resources.
  3. If these exceptions are not handled properly, the program terminates abruptly and may cause severe consequences.
  4. For example, the network connections, database connections and files may remain opened; database and file records may be left in an inconsistent state.
  5. Java has a built-in mechanism for handling runtime errors, referred to as exception handling.
  6. This is to ensure that you can write robust programs for mission-critical applications.

2. Drawbacks of exception handling in ‘C’ language.
  1. The programmers are not made to aware of the exceptional conditions.
  2. For example, the file to be opened may not necessarily exist. The programmer therefore did not write codes to test whether the file exists before opening the file.
  3. Suppose the programmer is aware of the exceptional conditions, he/she might decide to finish the main logic first, and write the exception handling codes later – this "later", unfortunately, usually never happens. In other words, you are not force to write the exception handling codes together with the main logic.
  4. Suppose the programmer decided to write the exception handling codes, the exception handling codes intertwine with the main logic in many if-else statements. This makes main logic hard to follow and the entire program hard to read. For example,

if (file exists) {
  open file;
  while (there is more records to be processed) {
     if (no IO errors) {
        process the file record
     } else {
        handle the errors
     }
  }
  if (file is opened) close the file;
} else {
  report the file does not exist;
}
3. How Java overcomes these drawbacks of ‘c’ language exception handling.
  1. You will be informed of the exceptional conditions that may arise in calling a method - Exceptions are declared in the method's signature.
  2. You are forced to handle exceptions while writing the main logic and cannot leave them as an afterthought - Your program cannot compiled without the exception handling codes.
  3. Exception handling codes are separated from the main logic - Via the try-catch-finally construct.
readFile {
   try {
       open the file;
       determine its size;
       allocate that much memory;
       read the file into memory;
       close the file;
   } catch (fileOpenFailed) {
      doSomething;
   } catch (sizeDeterminationFailed) {
       doSomething;
   } catch (memoryAllocationFailed) {
       doSomething;
   } catch (readFailed) {
       doSomething;
   } catch (fileCloseFailed) {
       doSomething;
   }
}

4. What is method call stack?
  1. A typical application involves many levels of method calls, which is managed by a so-called method call stack.
  2. A stack is a last-in-first-out queue. In the following example, main() method invokes methodA(); methodA() calls methodB(); methodB() calls methodC().
public class MethodCallStackDemo {
  public static void main(String[] args) {
     System.out.println("Enter main()");
     methodA();
     System.out.println("Exit main()");
  }

  public static void methodA() {
     System.out.println("Enter methodA()");
     methodB();
     System.out.println("Exit methodA()");
  }

  public static void methodB() {
     System.out.println("Enter methodB()");
     methodC();
     System.out.println("Exit methodB()");
  }

  public static void methodC() {
     System.out.println("Enter methodC()");
     System.out.println("Exit methodC()");
  }
}

Output :
Enter main()
Enter methodA()
Enter methodB()
Enter methodC()
Exit methodC()
Exit methodB()
Exit methodA()
Exit main()

  1. Flow of execution is
    1. JVM invoke the main().
    2. main() pushed onto call stack, before invoking methodA().
    3. methodA() pushed onto call stack, before invoking methodB().
    4. methodB() pushed onto call stack, before invoking methodC().
    5. methodC() completes.
    6. methodB() popped out from call stack and completes.
    7. methodA() popped out from the call stack and completes.
    8. main() popped out from the call stack and completes. Program exits.
                                   
5. How an Exception will occur in a program, default how it will print in console.
  1. When user enters an invalid input it may cause exception.
public class MethodCallStackDemo {
  public static void main(String[] args) {
     System.out.println("Enter main()");
     methodA();
     System.out.println("Exit main()");
  }

  public static void methodA() {
     System.out.println("Enter methodA()");
     methodB();
     System.out.println("Exit methodA()");
  }

  public static void methodB() {
     System.out.println("Enter methodB()");
     methodC();
     System.out.println("Exit methodB()");
  }

  public static void methodC() {
     System.out.println("Enter methodC()");
 System.out.println(1/0);//divide-by-0 trigger an ArithmeticException
     System.out.println("Exit methodC()");
  }
}
Output :
Enter main()
Enter methodA()
Enter methodB()
Enter methodC()
Exception in thread "main" java.lang.ArithmeticException: / by zero
       at MethodCallStackDemo.methodC(MethodCallStackDemo.java:22)
       at MethodCallStackDemo.methodB(MethodCallStackDemo.java:16)
       at MethodCallStackDemo.methodA(MethodCallStackDemo.java:10)
       at MethodCallStackDemo.main(MethodCallStackDemo.java:4)




No comments:

Post a Comment

3. Java Program to create Binary Tree