[!INDEX]
- Points and Definition.
- finally block will not execute if ( exit, fatal, e/x final, death)
- syntax try-catch-finally syntax
- try-catch-finally and no arithmetic ex. ( div/2 mt() )
- try-catch-finally and + arithmetic ex. ( above code )
- try-finally + arithmetic ex. + error ( above code )
- try-finally with arithmetic exception occur + rest code
- use of finally ( resource )
1. Points and Definition
[!NOTE]
- E/x occure = try-catch-finally.
- E/x not ocure = try-finally.
- Finally is the block that is always executed whether E/x is handled or not
- directly finally will not execute.
- We can use multiple catch blocks with one try block but we can only use
- single finally block with one try block, not multiple.
- The statements present in the finally block execute even if the try block
- 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]
- close resource in finally block
- if try to close in try block and some error occurred , then code jump to catch block and without closing the resource
- better to use finally block to close it