我的问题是我无法使内部 catch 抛出异常。 这里我希望 ex 被扔到外层捕获处。
Runnable(){
public void run(){
try{
try{
}catch(Exception ex){
System.out.println("Inner");
throw ex; //I get an error here
}
}catch(Exception e){
System.out.println("Outer");
}}
错误消息:必须捕获未报告的异常,并声明抛出
最佳答案
将 block 放入函数中......
private void myFunction() throws Exception {
try{
}catch(Exception ex){
System.out.println("Inner");
throw ex; //I get an error here
}
}
然后从代码块中调用它
Runnable(){
public void run(){
try{
myFunction();
}catch(Exception e){
System.out.println("Outer");
}}
关于java - 在线程中的 Catch 中抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18784834/