What is the difference between the 3 catch block variants in C# ( 'Catch', 'Catch (Exception)', and 'Catch(Exception e)' )?

前端 未结 7 761
无人共我
无人共我 2021-02-02 11:36

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

The MSDN article on try-catch uses 2 of them in its examples, but do

7条回答
  •  别跟我提以往
    2021-02-02 12:13

    In short...

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

    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 (Exception ex) catches all exceptions and provides a means to address it via the ex variable.

    Read more: http://msdn.microsoft.com/en-us/library/ms173160.aspx

提交回复
热议问题