vb.net 同时捕获多个异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14889560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:21:08  来源:igfitidea点击:

Catch multiple exceptions at the same time

vb.netexceptiontry-catch

提问by SysDragon

There is sometimes that one want to do the same on two different types of Exception. I searched, but I didn't find anything for VB.NET.

有时人们想对两种不同类型的异常做同样的事情。我进行了搜索,但没有找到有关 VB.NET 的任何内容。

Simple example:

简单的例子:

Try
    '...
Catch ex As Net.WebException
    'Do something
Catch ex As Net.CookieException
    'Do the same
Catch ex As Exception
    '...
End Try

I wonder if there is a way to catch both exceptions at once without needed to repeat code.

我想知道是否有一种方法可以同时捕获两个异常而无需重复代码。

回答by SysDragon

As seen on Catch multiple exceptions at once?it can be done this way:

正如在一次捕获多个异常中看到的那样可以这样做:

Catch ex As Exception When TypeOf ex Is FormatException OrElse TypeOf ex Is OverflowException