Catch multiple exceptions at once?

前端 未结 27 2298
夕颜
夕颜 2020-11-22 11:31

It is discouraged to simply catch System.Exception. Instead, only the "known" exceptions should be caught.

Now, this sometimes leads to unnecce

27条回答
  •  情深已故
    2020-11-22 12:18

    @Micheal

    Slightly revised version of your code:

    catch (Exception ex)
    {
       Type exType = ex.GetType();
       if (exType == typeof(System.FormatException) || 
           exType == typeof(System.OverflowException)
       {
           WebId = Guid.Empty;
       } else {
          throw;
       }
    }
    

    String comparisons are ugly and slow.

提交回复
热议问题