[!INDEX]

  1. Points and Definition.
  2. finally block will not execute if ( exit, fatal, e/x final, death)
  3. syntax try-catch-finally syntax
  4. try-catch-finally and no arithmetic ex. ( div/2 mt() )
  5. try-catch-finally and + arithmetic ex. ( above code )
  6. try-finally + arithmetic ex. + error ( above code )
  7. try-finally with arithmetic exception occur + rest code
  8. use of finally ( resource )

1. Points and Definition

[!NOTE]

  1. E/x occure = try-catch-finally.
  2. E/x not ocure = try-finally.
  3. Finally is the block that is always executed whether E/x is handled or not
  4. directly finally will not execute.
  5. We can use multiple catch blocks with one try block but we can only use
  6. single finally block with one try block, not multiple.
  7. The statements present in the finally block execute even if the try block
  8. contains control transfer statements (i.e. jump statements) like return,break
    or continue.

2. finally block will not execute if ( exit, fatal, e/x final, death)

[!NOTE] Case 1: Using of the System. Exit method.
Case 2: Causing a fatal error that causes the process to abort
Case 3: Due to an exception arising in the finally block
Case 4: The death of a Thread


3. syntax try-catch-finally syntax

    try{  
    // risky code (file open)  
    }catch(Exception e){  
    // handle code  
    }finally{  
    // clean up code (data connection close,  
    // open file close, memeory release)  
    // resource open in try must closei  
    // close
    }

4. try-catch-finally and no arithmetic ex. ( div/2 mt() )

package package2;
public class Main1 {  
        public static void main(String[] args) {
            try{
                int a=100, b=2, c;
                c=a/b;
                System.out.println(c);
            }catch(ArithmeticException e){
                System.out.println(e);
            }finally{
               System.out.println("Finally Block Executed, with no exception code ");
            }
        }
    }
50
Finally Block Executed, with no exception code 

5. try-catch-finally and + arithmetic ex. ( above code )

package package2;
public class Main1 {  
        public static void main(String[] args) {
            try{
                int a=100, b=0, c;
                c=a/b;
                System.out.println(c);
            }catch(ArithmeticException e){
                System.out.println(e);
            }finally{
               System.out.println("Finally Block Executed, with no exception code ");
            }
        }
    }
java.lang.ArithmeticException: / by zero
Finally Block Executed, with no exception code 

6. try-finally with arithmetic ex. + error ( above code )

package package2;
public class Main1 {  
        public static void main(String[] args) {
            try{
                int a=100, b=0, c;
                c=a/b;
                System.out.println(c);
            }finally{
               System.out.println("Finally Block Executed, with no exception code ");
            }
        }
    }
Finally Block Executed, with no exception code 
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at package2.Main1.main(Main1.java:6)

7. try-finally with arithmetic exception occur + rest code

package package2;
public class Main1 {  
        public static void main(String[] args) {
            try{
                int a=100, b=0, c;
                c=a/b;
                System.out.println(c);
            }finally{
               System.out.println("Finally Block Executed, with no exception code ");
            }
            System.out.println("another code body");// will nnt execute
        }
    }
Finally Block Executed, with no exception code 
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at package2.Main1.main(Main1.java:6)

8. use of finally ( resource )

[!NOTE]

  1. close resource in finally block
  2. if try to close in try block and some error occurred , then code jump to catch block and without closing the resource
  3. better to use finally block to close it