C# best practice error handling and passing error messages

后端 未结 4 502
独厮守ぢ
独厮守ぢ 2021-01-15 02:28

I am using asp.net and I am always struggling for a neat way to handle errors and if there are any, to pass the errormessage to the user. For example, I have an User class,

4条回答
  •  鱼传尺愫
    2021-01-15 03:20

    If you don't want to use the usual try/catch and display error divs in the catch then there is a Page.Error event which you could use to catch handle errors with the context of your page. Within that you would call Server.GetLastError() to get the last exception thrown and then inform the user something went wrong by directing them to an error page, or by showing/hiding error elements on your page.

提交回复
热议问题