Java 陷入困境
我的问题是我不能使内部catch抛出异常。 在这里,我想把前男友扔到外边Java 陷入困境,java,multithreading,exception,try-catch,throw,Java,Multithreading,Exception,Try Catch,Throw,我的问题是我不能使内部catch抛出异常。 在这里,我想把前男友扔到外边 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"); }
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");
}}
错误消息:必须捕获未报告的异常,并声明将其抛出将块放入函数中
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");
}}
将块放入函数中
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");
}}
这似乎功能正常。制作嵌套的try-catch块被认为是不好的做法
public class Test implements Runnable {
public void run() {
try {
try {
throw new IOException("bad"); //throw something here
} catch (Exception ex) {
System.out.println("Inner");
throw ex;
} finally {
}
} catch (Exception e) {
System.out.println("Outer");
}
}
public static void main(String[] args) {
new Test().run();
}
}
// PRINTS:
// Inner
// Outer
这似乎功能正常。制作嵌套的try-catch块被认为是不好的做法
public class Test implements Runnable {
public void run() {
try {
try {
throw new IOException("bad"); //throw something here
} catch (Exception ex) {
System.out.println("Inner");
throw ex;
} finally {
}
} catch (Exception e) {
System.out.println("Outer");
}
}
public static void main(String[] args) {
new Test().run();
}
}
// PRINTS:
// Inner
// Outer
试试这个
Callable(){
public void call(){
try{
try{
}catch(Exception ex){
System.out.println("Inner");
throw ex; //I get an error here
}
}catch(Exception e){
System.out.println("Outer");
}}
试试这个
Callable(){
public void call(){
try{
try{
}catch(Exception ex){
System.out.println("Inner");
throw ex; //I get an error here
}
}catch(Exception e){
System.out.println("Outer");
}}
请为你的例子发布更多信息,它的模板是所有的代码吗?您发布的内容不应产生未捕获的异常。e和ex都属于异常类型。没有专门化。请为您的示例发布更多信息,它只是模板。所有代码都是这样吗?您发布的内容不应产生未捕获的异常。e和ex都属于异常类型。没有专门知识。。