C#中的3个catch块变体('Catch','Catch(Exception)'和'Catch(Exception e)')有什么区别? [英] What is the difference between the 3 catch block variants in C# ( 'Catch', 'Catch (Exception)', and 'Catch(Exception e)' )?

查看:174
本文介绍了C#中的3个catch块变体('Catch','Catch(Exception)'和'Catch(Exception e)')有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,'Catch','Catch(Exception)'和'Catch(Exception e)'有什么区别?

In C#, what is the difference Between 'Catch', 'Catch (Exception)', and 'Catch(Exception e)' ?

有关try-catch的MSDN文章在其示例中使用了其中的2个,但没有

The MSDN article on try-catch uses 2 of them in its examples, but doesn't explain the difference in usage purposes.

try
{}
catch 
{}

try 
{}
catch (Exception)
{}

try
{}
catch(Exception e)
{}

这些有何不同?

推荐答案

简而言之...

Catch 将收到任何异常,但没有提供解决方法。

Catch without a parameter will receive any exception but provide no means to address it.

Catch(Exception)基本上会做同样的事情,因为您已指定根 Exception 类型。与 Catch(IOException)相对,后者仅捕获 IOException 类型。

Catch (Exception) will essentially do the same thing, because you've specified the root Exception type. As opposed to Catch (IOException) which would only catch the IOException type.

Catch(异常例外)捕获所有异常,并提供一种通过 ex 解决异常的方法

Catch (Exception ex) catches all exceptions and provides a means to address it via the ex variable.

了解更多: http://msdn.microsoft.com/zh-CN/library/ms173160.aspx

这篇关于C#中的3个catch块变体('Catch','Catch(Exception)'和'Catch(Exception e)')有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆