Best practices for using Try / Catch blocs

Asked

Viewed 293 times

1

What is the best strategy to use Try / Catch blocks:

try {
   //FAÇA ALGO
} 
catch(Exception ex){
  //LOGAR O ERRO
  //LANÇAR O ERRO PARA CIMA?
}

Example:

A tax calculation operation starts the process by the UI by triggering a one button event. This event will call an application method where you will process the data and perform the proper operations. Let’s assume that the transaction as a whole will only be satisfied if the whole process goes well, ie data persistence in the database, and common operations.

If an exception occurs in a method that makes a calculation before the information is persisted or an exception in the persistence of the data, what should be done?

  • Launch all exceptions up by logging into each internal method and display the error on the screen?
  • How to undo the entire transaction if the exception happens at an execution point of the transaction?
  • Possible duplicate of Try/Catch blocks

  • I closed as wide by the breadth of problems in it :) It depends on many details to make a decision, depends on the style of each. Maybe it’s not the case to release exceptions. Undo transactions can write a book.

  • @bigown could exemplify a detail to improve the question?

  • 2

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

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.