1. What is an Exception?
- An exception is an abnormal event that arises during the execution of the program and disrupts the normal flow of the program.
- Exception will arise when user enters invalid input, loss of resources.
- If these exceptions are not handled properly, the program terminates abruptly and may cause severe consequences.
- For example, the network connections, database connections and files may remain opened; database and file records may be left in an inconsistent state.
- Java has a built-in mechanism for handling runtime errors, referred to as exception handling.
- This is to ensure that you can write robust programs for mission-critical applications.
2. Drawbacks of exception handling in ‘C’ language.
- The programmers are not made to aware of the exceptional conditions.
- 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.
- 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.
- 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.
- You will be informed of the exceptional conditions that may arise in calling a method - Exceptions are declared in the method's signature.
- 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.
- 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?
- A typical application involves many levels of method calls, which is managed by a so-called method call stack.
- 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() |
- Flow of execution is
- JVM invoke the main().
- main() pushed onto call stack, before invoking methodA().
- methodA() pushed onto call stack, before invoking methodB().
- methodB() pushed onto call stack, before invoking methodC().
- methodC() completes.
- methodB() popped out from call stack and completes.
- methodA() popped out from the call stack and completes.
- 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.
- 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