当然可以。做成一个类,到时都调用不就行了?
例如: public void closeConnection() {
try {
connection.close();
} catch (Exception e) {
handleException(e);
}
} public void handleException(Exception e) {
JFrame nf = new JFrame();
JOptionPane.showMessageDialog(nf, "操作失败:"+e.getMessage() ,"连接数据库失败",JOptionPane.ERROR_MESSAGE);
;
if (e instanceof SQLException) {
while ( (e = ( (SQLException) e).getNextException()) != null) {
System.out.println(e);
}
}
}
例如: public void closeConnection() {
try {
connection.close();
} catch (Exception e) {
handleException(e);
}
} public void handleException(Exception e) {
JFrame nf = new JFrame();
JOptionPane.showMessageDialog(nf, "操作失败:"+e.getMessage() ,"连接数据库失败",JOptionPane.ERROR_MESSAGE);
;
if (e instanceof SQLException) {
while ( (e = ( (SQLException) e).getNextException()) != null) {
System.out.println(e);
}
}
}
你如果不想单独处理异常,直接把那些可能抛出异常的方法声明为throws Exception,而你的程序肯定有一个入口的比如main,在那里try catchpublic class A{
public void a() throws Exception{
...//some methods that might throw Exceptions;
} public void b() throws Exception{a();.....}
public void main(String[] args){
try{
b();
}catch(IOException e){
System.out.println("This is an IOException:"+e.getMessage());
}catch(Exception e){
e.printStackTrace();
}
}
}另外在GUI编程中,一般的Exception,即使不捕捉,一般也不会出问题
newman:理解力有问题。
单独处理IOException? shine333(shine) 只是做个例子。