Catching base Exception class in .NET

前端 未结 16 1963
心在旅途
心在旅途 2020-12-05 19:06

I keep hearing that

catch (Exception ex)

Is bad practise, however, I often use it in event handlers where an operation may for example go

16条回答
  •  青春惊慌失措
    2020-12-05 19:20

    The bad practice is

    catch (Exception ex){}
    

    and variants:

    catch (Exception ex){ return false; }
    

    etc.

    Catching all exceptions on the top-level and passing them on to the user (by either logging them or displaying them in a message-box, depending on whether you are writing a server- or a client-application), is exactly the right thing to do.

提交回复
热议问题