Recently I have developed and written a lot of warnings in Java. I am not used to so many warnings when the Finally block is not properly completed, so I will adjust it. Reference Code As follows:
// Obtain the serial number
Public static long get_seq (string seq, string sdatalink ){
Connection conn = dB. getconn ();
Preparedstatement stmt = NULL;
Resultset rs = NULL;
Long lseq =-1;
Try {
Stmt = conn. preparestatement ("select" + seq +
". Nextval iseq from dual" + sdatalink );
Rs = stmt.exe cutequery ();
If (Rs. Next ()){
Lseq = Rs. getlong ("iseq ");
}
}
Catch (exception e ){
Lseq =-1;
System. Out. println ("in pubbusi. get_seq (" + seq + "):" + E );
}
Finally {
DB. closestate (RS, stmt );
DB. closeconnect (conn );
Return (lseq );
}
}
Cause: It is reported that the Finally block is not completed properly. The reason is that return should not be written in finally.
Finally {
DB. closestate (RS, stmt );
DB. closeconnect (conn );
Return (lseq );
}
Change
Finally {
DB. closestate (RS, stmt );
DB. closeconnect (conn );
}
Return (lseq );
So there will be no such warning.
In fact, no matter whether the content in try is incorrect or not, the final value returned by getints is always 0.
Test code. The common practice is similar.
Try {
......
Return 1;
} Catch (exception e ){
E. printstacktrace ();
} Finally {
}
Return 0;
}
If an error occurs before return 1, the system enters catch and finally, and finally jumps out of try-catch-finally. 0 is returned.
If no error exists in try, the system enters finally and returns 1.
This is a feature of eclipse3, that is, the return statement should not exist in the Finally block.
It's just a warning.
Do not view the alert settings: In eclipse, find 'Finally 'does not complete normally in window-> preferences-> JAVA-> compiler-> errors/warnings-> potential Programming Problems and change its warning level to ignore. yes.
Is return correct in finally?
Correct but not recommended, because the normal return statement in try {} cannot be executed.