Java warning: the Finally block is not properly completed

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.