Using catch without arguments

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

What is the difference between:

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

and:

catch (Exception ex)
{
    MessageBox.Show(         


        
5条回答
  •  时光说笑
    2020-12-04 23:49

    Generally you should catch specific errors first.

    But if you go for catching a general Exception like you do I'd say use the second case:

    catch (Exception ex)
    {
         MessageBox.Show("Error.");
         //we never use ex, so is it better to use catch without arguments?
    }
    

    this can help you with debbuging since the variable contains the stack trace, exception message...etc. Which you can use for logging the error or something that will help you preventing it.

    Be very carefull using this approach, though:

    MessageBox.Show("Error.");
    

    Not keeping track of your errors somewhere(like a log file) can cause a really big mess.

提交回复
热议问题