Vb.net 试挡

Vb.net 试挡,vb.net,exception-handling,Vb.net,Exception Handling,我有以下代码 Try 'Some code that causes exception Catch ex as ExceptionType1 'Handle Section - 1 Catch ex as ExceptionType2 'Handle section - 2 Catch ex as ExceptionType3 'Handle section - 3 Finally ' Clean up End Try 假设ExceptionTy

我有以下代码

Try
    'Some code that causes exception
Catch ex as ExceptionType1
    'Handle Section - 1
Catch ex as ExceptionType2
    'Handle section - 2
Catch ex as ExceptionType3
    'Handle section - 3    
Finally
    ' Clean up
End Try

假设ExceptionType1由段-1处理的代码抛出。在第1节中处理之后,我可以将控制权转移到第2节/第3节吗?这可能吗?

更改代码以捕获一个块中的所有异常,并从中确定类型和执行路径

更改代码以捕获一个块中的所有异常,并从中确定类型和执行路径

我认为如果您使用嵌套的try块,您可以获得所需的行为。抛出异常后,执行转到catch块。如果没有任何东西被收回,它将继续到最后

我认为如果您使用嵌套的try块,您可以获得所需的行为。抛出异常后,执行转到catch块。如果没有任何东西被收回,它将继续到最后

您可以在异常处理程序中调用函数

Try
'Some code that causes exception'
Catch ex as ExceptionType1
  handler_1()
  handler_2()
  handler_3()
Catch ex as ExceptionType2
  handler_2()
  handler_3()
Catch ex as ExceptionType3
  handler_3()
Finally
  handler_4()    
End Try

您可以在异常处理程序中调用函数

Try
'Some code that causes exception'
Catch ex as ExceptionType1
  handler_1()
  handler_2()
  handler_3()
Catch ex as ExceptionType2
  handler_2()
  handler_3()
Catch ex as ExceptionType3
  handler_3()
Finally
  handler_4()    
End Try
你没有指定语言,我也不知道语言,所以我一般回答

你不能那样做。如果您想拥有通用代码,请将其放入finally,或者如果只需要为某些捕获案例执行,则可以将该代码复制到相应的案例中。如果代码较大,并且希望避免冗余,则可以将其放入自己的函数中。如果这样可以降低代码的可读性,那么可以在爪哇和C++中至少嵌套您的Test/catch块。我不懂你的语言。以下是Java中的一个示例:

class ThrowingException {
    public static void main(String... args) {
        try {
            try {
                throw new RuntimeException();
            } catch(RuntimeException e) {
                System.out.println("Hi 1, handling RuntimeException..");
                throw e;
            } finally {
                System.out.println("finally 1");
            }
        } catch(Exception e) {
            System.out.println("Hi 2, handling Exception..");
        } finally {
            System.out.println("finally 2");
        }
    }
}
这将打印出:

Hi 1, handling RuntimeException..
finally 1
Hi 2, handling Exception..
finally 2
将您的通用代码放入外部catch块。使用嵌套版本也可以处理在没有显式地在catch块中重新抛出旧版本的情况下发生异常的情况。它可能更适合你想要的,但也可能不适合

您没有指定语言,我也不知道该语言,所以我一般回答

你不能那样做。如果您想拥有通用代码,请将其放入finally,或者如果只需要为某些捕获案例执行,则可以将该代码复制到相应的案例中。如果代码较大,并且希望避免冗余,则可以将其放入自己的函数中。如果这样可以降低代码的可读性,那么可以在爪哇和C++中至少嵌套您的Test/catch块。我不懂你的语言。以下是Java中的一个示例:

class ThrowingException {
    public static void main(String... args) {
        try {
            try {
                throw new RuntimeException();
            } catch(RuntimeException e) {
                System.out.println("Hi 1, handling RuntimeException..");
                throw e;
            } finally {
                System.out.println("finally 1");
            }
        } catch(Exception e) {
            System.out.println("Hi 2, handling Exception..");
        } finally {
            System.out.println("finally 2");
        }
    }
}
这将打印出:

Hi 1, handling RuntimeException..
finally 1
Hi 2, handling Exception..
finally 2

将您的通用代码放入外部catch块。使用嵌套版本也可以处理在没有显式地在catch块中重新抛出旧版本的情况下发生异常的情况。它可能更适合你想要的,但也可能不适合

你能把你想做那件事的目的贴出来吗?你能把你想做那件事的目的贴出来吗?