ASP.Net Page Method exceptions not bubbling up? - asp.net

I can't figure out why the exception from a static ASP.Net Page method isn't bubbling up to the Application_Error event to be handled in the global.asax file. I'm not really expecting any errors, but I would like to be safe and know about them if they do happen to occur and would rather not wrap every static method in a try...catch.
Does anyone know how to catch these exceptions or at least why they aren't bubbling?

Exceptions bubble to the error handler in Application_Error if they are not being handled in the lower layers. If you already have a TRY/CATCH block where you think the exception is occurring, it will be trapped at that point.
Please post the code of your static method and your Application_Error. It will make it easier to provide you with an accurate answer instead of a generalized one.

If developing in Visual Studio, you should debug your code and then step through the source to find the exception and see what is catching it before bubbling up to your Application_Error method.

Related

Difference between ELMAH and Global.asax exception handling

I have website and I need to log exceptions globally, cannot understand what is difference between using ELMAH as error logging mechanism or use Application_Error in Global.asax, I think that all will log all exceptions thrown, or I'm missing something about ELMAH
ELMAH subscribes to the same Error event that Application_Error in Global.asax is designed to handle. As a result, you will not be at any more disadvantage with Application_Error than ELMAH as far as getting notified when an unhanded exception bubbles up to ASP.NET during an HTTP transaction. The difference is that except for very simplistic logging, if your needs ever expand to configurability, filtering errors, generating RSS feeds or building a browsing UI around logged errors then you'll pretty much end up writing the same code in your Application_Error as ELMAH gives you. If you plan to use that code in Application_Error of other web applications, then it will need to be generalised and rendered pluggable and that, once again, will give you what ELMAH already is.

How to catch unhandled exceptions in an asp.net application?

On a .NET exam I encountered this question.
Which of the following can you use to catch unhandled exceptions in an application?
Server_Error
Page_Error
Application_Error
Response_Error
OnError event
I know its Application_Error, But what I want to know is what are the others. By some google search I found OnError can be used to catch any errors. Still I am not sure about it. Could you tell what are the other ways to catch unhandled exception
The correct point to catch your unknown errors is the Application_Error.
Avoid to capture the OnError on your page, and let the system transfer it to the Application_Error because there you have lost the control on the page, so what other you can do if not transfer it to some error page ? - if you try to just reload it you have the problem to be in a close loop that can cause stack overflow.
From my experience I have issue when I try to handle errors using the page OnError and I use it only when I have to free some global memory, or something like that if an error happens on page.
To give a summary, try to catch all your errors inside a try/catch block, and give a message to your user / or just take care of this issues, but let the unknown errors to the global catcher to log it and fix it. The unknown errors is something that make you lose the real control of your program, and actually you do not know what to do because you do not have predict it - so log it and fix it for the next time
More details about the errors :
How do I make a "generic error" page in my ASP.NET application so that it handles errors triggered when serving that page itself?

VB How to generically implement exception logging if I have already got many try-catch block

I am writing ASP.Net Web-form in VB.
I have a system which have already handled exceptions by try-catch blocks everywhere.
How do I quickly and generically implement exception logging like overriding something instead of inserting logging functions in every single catch block?
Using an AOP framework like Postsharp you can hook on exceptions thrown and log a message, but that will only work if you haven't already handled them in your code.
Your best bet would be to refactor your exception handling code. Apart from the global catch-all exception handling, you should only have try-catch blocks in places where you can actually handle the exception. And in those cases you probably want to add a specific message in the log telling why and how this exception was handled.

ASP.Net Exceptions - Catch in Page or handle in Global.asax (Application_Error)

Looking for best practice focused answers here with explanations.
Should the presentation layer of an ASP.Net app catch and handle exceptions thrown from the business layer, or should these be allowed to bubble out, where they can all be logged and handled uniformly in the Global.ascx's Application_Error handler?
ie..
protected void Application_Error(object sender, EventArgs e)
{
logExceptionDetails(Server.GetLastError());
HttpContext.Current.Server.Transfer("~/Error.aspx");;
}
Thanks
My approach to Exceptions is to let them happen and log them with Elmah, and use the built-in Custom Error Page mechanism to notify my user that something went wrong.
All of that can be done with zero code (configured in web.config).
Well, if you need to truely "handle" the exception, then you'll need try-catch blocks in your code where the exceptions might happen.
The method you choose depends on how often you expect the event to occur. If the event is truly exceptional and is an error (such as an unexpected end-of-file), using exception handling is better because less code is executed in the normal case. If the event happens routinely, using the programmatic method to check for errors is better.
However, if you're looking at logging exceptions that have been "uncaught", then you should use Global's Error event like you've shown.This is the best practice. In fact if you implement this as an HttpModule then your Exception handling is non-intrusive and can plugged into other applications or removed by simply altering the web.config file.
Take a look at this article on 4GuysFromRolla
https://web.archive.org/web/20211020134127/https://www.4guysfromrolla.com/articles/081209-1.aspx

Server-side ASP.Net Ajax exception handling

I am using ASP.net's AJAX framework and I am trying to do some exception logging on the javascript-called webservice.
Looking through the web, I find some people handling them client-side and sending them back to the server. I'd prefer not doing that since it relies on an other server call (if it failed the first time, sending an other request doesn't look like a good idea and that makes me change all my calls)
I see some other people decorating all their methods with try... catch blocks (which leads to duplicate error handling, and yet an other try catch block).
However, I'm trying to achieve something similar to global.asax's Application_Error() which handles every uncaught exception (nope, it doesn't seem to be called for AJAX called methods exceptions).
Is that even possible?
Use ELMAH - it can capture ajax errors.

Resources