Using catch without arguments

前端 未结 5 1919
终归单人心
终归单人心 2020-12-04 23:12

What is the difference between:

catch
{
    MessageBox.Show(\"Error.\");
}

and:

catch (Exception ex)
{
    MessageBox.Show(         


        
5条回答
  •  臣服心动
    2020-12-05 00:01

    I think they are the same. But the second case raised a compiler warning because you declare an exception you didn't use. I rather like the first one because you say explicitly that you don't use the exception. There is also a third one

    catch (Exception)
    {
        //do something
    }
    

    if you want to specify the type of exception but doesn't care about the exception itself.

提交回复
热议问题