Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application_Error - GetLastError() or GetLastError().GetBaseException()

When handling an error in Application_Error, which of those two should I use?

I'm finding multiple examples for both, but it's not really clear if one is better than the other. Are there cases where only one will show the correct error?

Also, I doubt this matters, but the application is using MVC 4.

like image 859
user247702 Avatar asked Feb 26 '13 09:02

user247702


People also ask

What is Server GetLastError () method?

The GetLastError method returns an ASPError Object describing the error condition that occurred. This method is available only before the . asp file has sent any content to the client. Copy. GetLastError( )

How does ASP net handle application error global ASAX?

You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web. config file.


1 Answers

It depends on what exactly do you need.

From the documenation of Exception.GetBaseException:

When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

Application_Error handles exceptions on the upper level, maybe after several exception handling mechanisms, so if exception is thrown like this:

try {
   //Lots of code, method calls, etc...
   try {
       throw new FooException("Foo");
   } catch(FooException fe) {
       throw new BarException("Bar", fe);
   }
}catch(BarException be) {
    throw new FooBarException("FooBar", be);
}

then GetLastError will get you FooBarException, while GetLastError().GetBaseException() will get you FooException. So the former returns actual unhandled exception while the latter returns the root cause.

I assume that Foo, Bar and FooBar exception classes do not override GetLastError or InnerException

like image 86
default locale Avatar answered Sep 29 '22 11:09

default locale