Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I place try catch?

Learning to log errors. This is a basic structure of code all through my project.

I have been advised that the try block has to be placed only in Event Handlers. But when logging the error, it is required to know, which method caused the error. So, in such cases, should I also keep try block in AllIsFine() & SaveData(). If yes, then should it log the error or just throw.

What is the best/Standard practice.

DataContext objDataContext = new DataContext();
protected void btn_Click(object sender, EventArgs e)
{
  try
   {
     if(AllIsFine())
      {
        objDataContext.SaveData();
      }
   }
  catch(Exception ex)
   {
     //some handling
   }
}

private bool AllIsFine()
{
   //some code
}

EDIT: Ofcourse, we would try to see that it never raises an exception, but not practical. I am looking at it this way. When deployed, the only access I have is to the logs and need to get as much info as possible.So in such cases, (and with this kind of a structure), where do you advise to keep the try catch

like image 305
Qwerty Avatar asked Dec 14 '22 18:12

Qwerty


2 Answers

I have been advised that the try block has to be placed only in Event Handlers

This is not true.
There are different type of exceptions, some you need to handle, some you should throw, some you should catch, some you should not. Read this please.

Summary:

  • You should catch exceptions that you can do something with them
  • If you want to just log exceptions catch, log, and then use throw not throw ex (it resets the call stack)
  • Prevent exception from happening if you can, check for conditions that prevent exceptions(IndexOutOfRangeException, NullPointerException, ArgumentNullException) do that instead of executing the action and catch the exception
  • Don’t catch fatal exceptions; nothing you can do about them anyway, and trying to generally makes it worse.
  • Fix your code so that it never triggers a boneheaded exception – an "index out of range" exception should never happen in production code.
  • Avoid vexing exceptions whenever possible by calling the “Try” versions of those vexing methods that throw in non-exceptional circumstances. If you cannot avoid calling a vexing method, catch its vexing exceptions.
  • Always handle exceptions that indicate unexpected exogenous conditions; generally it is not worthwhile or practical to anticipate every possible failure. Just try the operation and be prepared to handle the exception.
like image 197
Hamid Pourjam Avatar answered Dec 31 '22 02:12

Hamid Pourjam


But when logging the error, it is required to know, which method caused the error.

You should use the call stack for that.

Try as much as possible to Prevent the Exceptions, and not catching them, if you would Catch an error, try to catch a specific Error, what I mean by that, not try to Catch Exception but something like specific like InvalidCastException.

After catching an Error you should log the exception, but not throwing an exception to log it.

like image 20
Bafla13 Avatar answered Dec 31 '22 00:12

Bafla13