Exception handling issues related to the keyword finally try ... catch ... finally in

1. The following code will not execute finally, if would return before or after?
public void Test(){
    int a = hello();
    System.out.println("a = " + a);
}

public int hello(){
    int a = 10;
    try{
        System.out.println("a = "+ a);
        return a++;
    } catch(Exception e){
        e.printStackTrace();
    } finally{
        System.out.println("a = " + a);
        a++;
        System.out.println("a = " + a);
    }
    return 5;
}

[A]: Whenever finally block will be executed before the execution and return .

2. The  difference between the final, finally in the finalize java () of

  • final modified class can not be inherited, final modification of the method can not be overridden, final modified variables are constant.
  • finally is a key exception is processed, usually try and catch statement can be used in conjunction, can also be used in conjunction with finally, it can also be used in conjunction with three. Representative finally Whenever (is abnormal) wherein the code to be executed are generally used for closing some resistance performance of resources such as database connection.
  • finalize () is a method of the Object class, is protected by a modified, so we subclass can override finalize () method. Its role is java will automatically call this method before the object to be recovered in the implementation of garbage collection, so you can define the operation of some recovery or clean up memory resources in this method.

Guess you like

Origin www.cnblogs.com/EaTerLeader/p/11078321.html