locked
Is it logical to use try catch for every code block RRS feed

  • Question

  • User283528319 posted

    to communicate user effectively and let him know if something goes wrong I am tended to use try catch block for every important line of code

    what do you say? waste of time? waste of resources or "yeah it is an obligation infact" 

    Friday, August 2, 2019 6:45 AM

Answers

  • User711641945 posted

    Hi fatihbarut,

    1.For conditions that are likely to occur but might trigger an exception, you could consider handling them in a way that will avoid the exception.

    For example,when you are using some external controls/dlls in your application and you are not sure about its functioning and you do not want an error in that external control to stop your own application then you can use Try/ Catch. It will not entirely stop your application because of the fatal error in that control. But the part of your code using that control would not function.

    Reference: https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions

    2.Finding and designing away exception-heavy code can result in a decent perf win. Bear in mind that this has nothing to do with try/catch blocks: you only incur the cost when the actual exception is thrown. You can use as many try/catch blocks as you want. 

    Reference: https://docs.microsoft.com/en-us/previous-versions/dotnet/articles/ms973839(v=msdn.10)#performance-tips-for-all-applications

    3.Try/Catch can generally be used to debug an application and after that you can remove the Try/Catch block.

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 9:11 AM
  • User1120430333 posted

    fatihbarut

    to communicate user effectively and let him know if something goes wrong I am tended to use try catch block for every important line of code

    what do you say? waste of time? waste of resources or "yeah it is an obligation infact" 

    I don't use try/catch period nowhere in any code in the solution, not with ASP.NET Core MVC or ASP.NET Core WebAPI. If an exception is thrown, then it is an unhandled exception. The Global Exception Handler catch's all unhandled exceptions, logs the exception to a logfile and redirects to a user friendly page about the exception.  I would then go to the logfile to review the exception message, stacktrace and inner.exception message.  

    Actually,  it's quite useful in debugging too.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 9:41 AM

All replies

  • User711641945 posted

    Hi fatihbarut,

    1.For conditions that are likely to occur but might trigger an exception, you could consider handling them in a way that will avoid the exception.

    For example,when you are using some external controls/dlls in your application and you are not sure about its functioning and you do not want an error in that external control to stop your own application then you can use Try/ Catch. It will not entirely stop your application because of the fatal error in that control. But the part of your code using that control would not function.

    Reference: https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions

    2.Finding and designing away exception-heavy code can result in a decent perf win. Bear in mind that this has nothing to do with try/catch blocks: you only incur the cost when the actual exception is thrown. You can use as many try/catch blocks as you want. 

    Reference: https://docs.microsoft.com/en-us/previous-versions/dotnet/articles/ms973839(v=msdn.10)#performance-tips-for-all-applications

    3.Try/Catch can generally be used to debug an application and after that you can remove the Try/Catch block.

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 9:11 AM
  • User1120430333 posted

    fatihbarut

    to communicate user effectively and let him know if something goes wrong I am tended to use try catch block for every important line of code

    what do you say? waste of time? waste of resources or "yeah it is an obligation infact" 

    I don't use try/catch period nowhere in any code in the solution, not with ASP.NET Core MVC or ASP.NET Core WebAPI. If an exception is thrown, then it is an unhandled exception. The Global Exception Handler catch's all unhandled exceptions, logs the exception to a logfile and redirects to a user friendly page about the exception.  I would then go to the logfile to review the exception message, stacktrace and inner.exception message.  

    Actually,  it's quite useful in debugging too.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 9:41 AM
  • User283528319 posted

    thanks

    Friday, August 2, 2019 12:16 PM
  • User283528319 posted

    fatihbarut

    to communicate user effectively and let him know if something goes wrong I am tended to use try catch block for every important line of code

    what do you say? waste of time? waste of resources or "yeah it is an obligation infact" 

    I don't use try/catch period nowhere in any code in the solution, not with ASP.NET Core MVC or ASP.NET Core WebAPI. If an exception is thrown, then it is an unhandled exception. The Global Exception Handler catch's all unhandled exceptions, logs the exception to a logfile and redirects to a user friendly page about the exception.  I would then go to the logfile to review the exception message, stacktrace and inner.exception message.  

    Actually,  it's quite useful in debugging too.

    looks very good approach, any documentation for it? may be I can go down that way.

    Friday, August 2, 2019 12:17 PM
  • User-474980206 posted
    In general you should not catch and throw. If the code can not handle any errors, and will just throw them, what’s the point of the catch?
    Friday, August 2, 2019 2:44 PM
  • User1120430333 posted

    DA924

    fatihbarut

    to communicate user effectively and let him know if something goes wrong I am tended to use try catch block for every important line of code

    what do you say? waste of time? waste of resources or "yeah it is an obligation infact" 

    I don't use try/catch period nowhere in any code in the solution, not with ASP.NET Core MVC or ASP.NET Core WebAPI. If an exception is thrown, then it is an unhandled exception. The Global Exception Handler catch's all unhandled exceptions, logs the exception to a logfile and redirects to a user friendly page about the exception.  I would then go to the logfile to review the exception message, stacktrace and inner.exception message.  

    Actually,  it's quite useful in debugging too.

    looks very good approach, any documentation for it? may be I can go down that way.

    This is what I used for ASP.NET MVC Core. For ASP.NET WebAPI, it's a different approach.

    Also, the part on how to do it in the Startup.cs, I ignored/removed it about development as opposed to non development environment. It all went the Error/Error no matter what. 

    https://scottsauber.com/2017/04/03/adding-global-error-handling-and-logging-in-asp-net-core/

    Friday, August 2, 2019 3:23 PM