java中在一个方法中可以使用多个try吗?

2025-05-06 12:00:52
推荐回答(1个)
回答1:

可以啊.自己用代码验证吧.
public void method(){
Connection conn = null;
try{
//注意这是不规范的语法
try {
conn = DriverManager.getConnection("");
} catch (SQLException e) {
e.printStackTrace();
}
}catch (Exception e) {
e.printStackTrace();
}
//以上方式最好这样
try{
conn = DriverManager.getConnection("");
} catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}

Statement state= null;
try {
state = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}