Skip to main content

Default Exception

  •  Inside a method if any exception occurs, the method in which it is raised is responsible to create an exception object by including the following the information.

                Name of exception
                Description of exception
                location at which exception occurs (stack trace)
               

  • After creating exception method handover that object to the JVM.
  • JVM will check whether the method contains any exception handling code or not. If the method doesn't contain exception handling code, then JVM terminates that method abnormally and removes the corresponding entry from the stack.
  • Then JVM identify caller method and checks whether caller method any handling code are not.
  • If the caller method doesn't contain handling code, then JVM terminates that caller method also abnormally and removes the corresponding entry from the stack

  • this process continue until main method and if the main method also doesn't contain handling code then JVM terminate main method also abnormally and remove corresponding entry from the stack.

  • Then JVM handovers responsibility of exception handling to default exception handler, which is the part of JVM default exception handler prints exception information in the following format and terminates program abnormally
         eg-:
              class DefaultException{
                    public static void main(String[] args){
                           dostuff();
                    }
                    public static void dostuff(){
                        doMoreStuff();
                    }
                    public static void doMoreStuff(){
                        System.out.print(10/0);
                    }
            }



          class DefaultException{
                    public static void main(String[] args){
                           dostuff();
                    }
                    public static void dostuff(){
                        doMoreStuff();
                        System.out.print(10/0);
                    }
                    public static void doMoreStuff(){
                        System.out.print("Hello");
                    }
            }

            output-: Hello
                        Exception at thread main
                                    at dostuff()
                                       and main()
           
            class DefaultException{
                    public static void main(String[] args){
                           dostuff();
                           System.out.print(10/0);
                    }
                    public static void dostuff(){
                        doMoreStuff();
                        System.out.print("Hi");
                    }
                    public static void doMoreStuff(){
                        System.out.print("Hello");
                    }
            }
             output-: Hello
                           Hi
                        Exception at thread main
                                    at main()


Note-: In a program if at least one method terminates abnormally then the program termination is abnormal termination.
    If All method terminates normally, then only program termination is normal termination.

Exception hierarchy

Throwable-: Throwable class access root for Java exception hierarchy.throwable class defines two child classes 
  1. Exception-: most of the time exceptions are caused by our program and these are recoverable for i.e if our programming requirement is to read data from remote file locating at Landon at run time remote file not available then we will get run time exception saying file not found exception. If a file not found exception occurs, we can provide a local file and continue the rest of the program normally.
          Example-:
                           try{
                                    read data from remote file location at Landon
                               }catch(FileNotFoundExceiption e){
                                    use locale file and continue rest of program normally
                              }
  2. Error-: most of the time Errors are not caused by our program and these are due to lack of system resources. Errors are non-recoverable. For example -; outofmemory error occurs being a programmer we can't do any thing and the program will be terminated abnormally. System admin or server admin is responsible to increase the heap memory. 





Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...