반응형
'최종 차단이 정상적으로 완료되지 않음'Eclipse 경고
Eclipse는 다음 코드에서 경고를 표시합니다.
public int getTicket(int lotteryId, String player) {
try {
c = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database, this.user, this.password);
int ticketNumber;
PreparedStatement p = c.prepareStatement(
"SELECT max(num_ticket) " +
"FROM loteria_tickets " +
"WHERE id_loteria = ?"
);
p.setInt(1, lotteryId);
ResultSet rs = p.executeQuery();
if (rs.next()) {
ticketNumber = rs.getInt(1);
} else {
ticketNumber = -1;
}
ticketNumber++;
p = c.prepareStatement(
"INSERT INTO loteria_tickets " +
"VALUES (?,?,?,?)");
p.setInt(1, lotteryId);
p.setInt(2, ticketNumber);
p.setString(3, player);
p.setDate(4, new java.sql.Date((new java.util.Date()).getTime()));
p.executeUpdate();
return ticketNumber;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
try {
c.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return -1;
}
}
내 코드에 어떤 문제가 있습니까?
그것에서 return 문을 제거하십시오. 최종 블록은 정리 블록으로 간주되며 일반적으로 반환이 예상되지 않습니다.
return
에서 finally
"재정의"더 예외 던지기.
public class App {
public static void main(String[] args) {
System.err.println(f());
}
public static int f() {
try {
throw new RuntimeException();
} finally {
return 1;
}
}
}
1
일반적으로 finally
블록은 다른 return
문 또는을 덮어 쓸 수 있으므로 return 문이 없어야 Exceptions
합니다.
자세한 내용과 배경에 대한 자세한 답변은 질문을 참조하십시오.
블록에 return
및 throw
문 이 모두 finally
있으면 경고가 표시됩니다. 예를 들어 다음 finally 블록과 동일한 경고가 표시됩니다.
...
}finally{
throw new RuntimeException("from finally!");
}
...
If you don't have any catch
blocks, then your finally
blocks have to be nested directly inside of each other. It's only catching an exception that allows your code to continue past the end of a try/catch/finally block. If you don't catch the exception, you can't have any code after a finally block!
You can see how this works with this example on Repl.it
Example Output
testing if 0 > 5 ?
try1
try2
finally3
catch1
finally2
After other finally
finally1
end of function
testing if 10 > 5 ?
try1
try2
try3
success
finally3
finally2
finally1
Code in example at Repl.it
class Main {
public static void main(String[] args) {
isGreaterThan5(0);
isGreaterThan5(10);
}
public static boolean isGreaterThan5(int a)
{
System.out.println();
System.out.println("testing if " + a + " > 5 ?");
try
{
System.out.println("try1");
try
{
System.out.println("try2");
try
{
if (a <= 5)
{
throw new RuntimeException("Problems!");
}
System.out.println("try3");
System.out.println("success");
return true;
}
finally
{
System.out.println("finally3");
}
}
catch (Exception e)
{
System.out.println("catch1");
}
finally
{
System.out.println("finally2");
}
System.out.println("After other finally");
}
catch (Exception e)
{
System.out.println("failed");
return false;
}
finally
{
System.out.println("finally1");
}
System.out.println("end of function");
return false;
}
}
반응형
'development' 카테고리의 다른 글
바로 가기에서 특정 디렉터리에서 Powershell 열기 (0) | 2020.11.10 |
---|---|
오류 mongod가 죽었지 만 subsys가 잠겨 있고 Linux에서 저널 파일을위한 여유 공간이 부족한 이유는 무엇입니까? (0) | 2020.11.10 |
python BeautifulSoup 구문 분석 테이블 (0) | 2020.11.10 |
WPF C # 경로 : 코드에서 지오메트리에 대한 경로 데이터가있는 문자열에서 가져 오는 방법 (XAML이 아님) (0) | 2020.11.10 |
F #의 유형 추론이 왜 그렇게 변덕 스럽습니까? (0) | 2020.11.10 |