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

前端 未结 5 730
借酒劲吻你
借酒劲吻你 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:15

    catch(Exception ex) can handle all exceptions which are derived from System.Exception class, however if the exception thrown is not derived from System.Exception then it will not be handled by catch(Exception ex).

    Prior to .NET 3.0 exceptions thrown by unsafe code (i.e. code not targeting CLR) were handled by catch{}. After .NET Framework 3.0 all types of exception can be handled by System.Exception class. catch{} is now obsolete .

提交回复
热议问题