Difference between catch(Exception), catch() and just catch

前端 未结 5 678
借酒劲吻你
借酒劲吻你 2021-01-17 10:53

I want to know if I can safely write catch() only to catch all System.Exception types. Or do I\'ve to stick to catch(Exception) to accomplish this. I know for other exceptio

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 11:03

    Both constructs (catch () being a syntax error, as sh4nx0r rightfully pointed out) behave the same in C#. The fact that both are allowed is probably something the language inherited from C++ syntax.

    Others languages, including C++/CLI, can throw objects that do not derive from System.Exception. In these languages, catch will handle those non-CLS exceptions, but catch (Exception) won't.

提交回复
热议问题