Exception handling in the Global.asax.cs file

I am maintaining an Asp.net web application. Instead of using try catch blocks in the C# code to wrap specific C# logic they put this exception handling code in the global.asax file.

Exception exception = server.GetLastError();
if(exception != null)
  exception = exception.GetBaseException()
  if(exception ! = null)
      zError.InnerHtml = exception.Message();
        // Code not shown here but write the error message to the exception log.

In my mind this seems a little less effective than using try catch blocks throughout the code in order to target a specific exception. Like for example suppose a SQLException occured. My thinking is that the above code would not log this as a sqlException, but as some more general type of exception. Am I correct in my thinking? What do other Experts thing about this?
Who is Participating?
Göran AnderssonCommented:
You should combine both ways of handling excpetions. Catch exceptions in the code that you expect and can handle, and leave the unexpected and unrecoverable exceptions to the fallback handling in the global.asax.
brgdotnetcontractorAuthor Commented:
Dear GreenGhost. You have helped me before and I deeply appreciate your  expert help becuase you give excellent information.

Your reply sounds good. If we were to be more specific though, am I correct in my thinking that the exception caught in the global asax file for the code I posted is not as specific as if I were to put
in specific catch code to catch a specific exception such as a SqlException,OutOfMemoryException,ArithmeticException etcetera?
Göran AnderssonCommented:
Yes, when catching exceptions close to the source, you would want to catch only the specific exceptions that you expect.

If you for example do a database call, you would only catch SqlException. If a totally different exception would somehow occur in that code, you wouldn't want to handle it as if it was an error message from the database. You should let that exception go to be handled by the code in global.asax, so that it's logged as an unexpected error.
brgdotnetcontractorAuthor Commented:
Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.