I have a three tier web app in which I am using MS Enterprise Library Exception Handling application block for exception management. The exceptions in the DataAccess and BusinessManagers are being logged and then replaced with a custom exception containing
a general message. This custom exception is then being thrown up till the UI layer. I have defined a global exception handler in the Application_Error event in the Global.asax file from where I am planning to redirect the user to a custom error page for a
user friendly error message display.
In my UI event handlers I am wrapping the BusinessManager calls in a try catch block and throwing the exception hoping to catch it in the global handler. While it does get caught in the global exception handler and displays the custom error page, I am getting
a debugger break in the throw statement in the Business Layer method catch block stating that "Exception was unhandled by user code". After this the code hits the global exception handler. Even if I remove the try catch block from the UI code, I am getting
the same unhandled error message from the place where I throw the exception from the BusinessManager. I am not sure where I am going wrong.
In my UI event handlers I am wrapping the BusinessManager calls in a try catch block and throwing the exception hoping to catch it in the global handler.
This is one of the biggest misconceptions about exception handling. There is no need to implement the following code:
Try
MyBusniessObject.DoSomething()
Catch (ex As Exception)
Throw ex
End Try
This is unneeded code that actually results in the stacktrace getting messed up as to where the original exception occurred. You can remove all of that type code and it will eventually get caught in your global error handler. The VS.NET IDE's behavior will
break on exception and highlight the offending line (in green by default color scheme) alerting the exception. Each time you continue execution, it will continue until the next handler is met.
If you have a bunch of Try-Catch-Throw statements, it will stop at each one. Otherwise the code will halt on the offending line that broke, and upon continuing execution will bubble up to the global error handler code defined to log the exception.
Maybe my question was not quite clear. Let me show with some sample code. Lets say we are talking about a typical UI-BLL-DAL ASP.NET solution.
Code for DAL:
public static class DAl
{
public static string DoDataAccess()
{
try
{
throw new Exception("Exception from Data Access Layer");
}
finally
{
}
return string.Empty;
}
}
Code for BLL:
public static class BLL
{
public static string DoBusinessLogic()
{
try
{
DAl.DoDataAccess();
}
catch (Exception ex)
{
throw new Exception("Exception from Business Logic Layer", ex);
}
finally
{
}
return string.Empty;
}
}
Code for UI(ASP.NET):
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
BLL.DoBusinessLogic();
}
catch (Exception ex)
{
throw new Exception("Could not load page", ex);
}
finally
{
}
}
}
Code for Global Exception Handler(Global.asax):
void Application_Error(object sender, EventArgs e)
{
HttpException ex = Server.GetLastError()as HttpException;
if (ex != null)
{
Response.Write(getExceptionMessages(ex.InnerException));
Response.Flush();
Response.End();
}
}
private string getExceptionMessages(Exception ex)
{
StringBuilder sbr = new StringBuilder();
do
{
sbr.Append(ex.Message);
sbr.Append("<BR>");
ex = ex.InnerException;
} while (ex != null);
return sbr.ToString();
}
Please note that the above solution is just a demo solution. The actual issue is when I am running this solution, the VS.NET IDE breaks at the offending line : throw new Exception("Could not load page", ex); in the default.aspx catch handler for the Page
Load method. My question is since I have a global handler defined why does the IDE assume that this code is throwing an exception which has no handlers defined but still it takes the application to the global handler.
Hope I was able to make the question more clear this time.
Simply put, the exception is not handled by your code. it is handled by other code (ASP.NET code), which then calls back to Application_Error for you.
And you have no Server.ClearError() to remove the error and indicate that you've dealt with it anyway. You might want to think about that. (I'm also assuming that this is just for fun ... as you'd never, ever want to show the user an exception message).
Do you, by any chance, have the debug setting "Just My Code" set? This might be causing you some grief.
Also, you might want to poke around with the Exceptions dialog. Have a look at http://msdn.microsoft.com/en-us/library/038tzxdw.aspx and reverse the logic, so that you don't break on user-unhandled exceptions.
Regards
Dave
Marked as answer by snkar on Mar 14, 2012 05:32 AM
You were absolutely correct about the Just My Code setting. That is what was creating the issue.Thanks a lot for pointing me in the right direction. There are so many features to Visual Studio that are not so specifically known till you encounter a need
to explicitly change it. Is there any resource available which can readily list at least the important features/tips/tricks of the IDE?
But I am not sure on when should this "Just My Code" setting be useful. I could think of only the situation when probably I am trying to debug my code but do not want to step into an external library which I might be using.
snkar
Member
5 Points
5 Posts
Throwing exception results in Debugger Break
Mar 07, 2012 04:38 AM|LINK
I have a three tier web app in which I am using MS Enterprise Library Exception Handling application block for exception management. The exceptions in the DataAccess and BusinessManagers are being logged and then replaced with a custom exception containing a general message. This custom exception is then being thrown up till the UI layer. I have defined a global exception handler in the Application_Error event in the Global.asax file from where I am planning to redirect the user to a custom error page for a user friendly error message display.
In my UI event handlers I am wrapping the BusinessManager calls in a try catch block and throwing the exception hoping to catch it in the global handler. While it does get caught in the global exception handler and displays the custom error page, I am getting a debugger break in the throw statement in the Business Layer method catch block stating that "Exception was unhandled by user code". After this the code hits the global exception handler. Even if I remove the try catch block from the UI code, I am getting the same unhandled error message from the place where I throw the exception from the BusinessManager. I am not sure where I am going wrong.
atconway
All-Star
16846 Points
2756 Posts
Re: Throwing exception results in Debugger Break
Mar 07, 2012 03:41 PM|LINK
This is one of the biggest misconceptions about exception handling. There is no need to implement the following code:
This is unneeded code that actually results in the stacktrace getting messed up as to where the original exception occurred. You can remove all of that type code and it will eventually get caught in your global error handler. The VS.NET IDE's behavior will break on exception and highlight the offending line (in green by default color scheme) alerting the exception. Each time you continue execution, it will continue until the next handler is met.
If you have a bunch of Try-Catch-Throw statements, it will stop at each one. Otherwise the code will halt on the offending line that broke, and upon continuing execution will bubble up to the global error handler code defined to log the exception.
snkar
Member
5 Points
5 Posts
Re: Throwing exception results in Debugger Break
Mar 13, 2012 06:42 AM|LINK
Maybe my question was not quite clear. Let me show with some sample code. Lets say we are talking about a typical UI-BLL-DAL ASP.NET solution.
Code for DAL:
public static class DAl { public static string DoDataAccess() { try { throw new Exception("Exception from Data Access Layer"); } finally { } return string.Empty; } }Code for BLL:
Code for UI(ASP.NET):
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { BLL.DoBusinessLogic(); } catch (Exception ex) { throw new Exception("Could not load page", ex); } finally { } } }Code for Global Exception Handler(Global.asax):
void Application_Error(object sender, EventArgs e) { HttpException ex = Server.GetLastError()as HttpException; if (ex != null) { Response.Write(getExceptionMessages(ex.InnerException)); Response.Flush(); Response.End(); } } private string getExceptionMessages(Exception ex) { StringBuilder sbr = new StringBuilder(); do { sbr.Append(ex.Message); sbr.Append("<BR>"); ex = ex.InnerException; } while (ex != null); return sbr.ToString(); }Please note that the above solution is just a demo solution. The actual issue is when I am running this solution, the VS.NET IDE breaks at the offending line : throw new Exception("Could not load page", ex); in the default.aspx catch handler for the Page Load method. My question is since I have a global handler defined why does the IDE assume that this code is throwing an exception which has no handlers defined but still it takes the application to the global handler.
Hope I was able to make the question more clear this time.
DMW
All-Star
15943 Points
2353 Posts
Re: Throwing exception results in Debugger Break
Mar 13, 2012 12:46 PM|LINK
Simply put, the exception is not handled by your code. it is handled by other code (ASP.NET code), which then calls back to Application_Error for you.
And you have no Server.ClearError() to remove the error and indicate that you've dealt with it anyway. You might want to think about that. (I'm also assuming that this is just for fun ... as you'd never, ever want to show the user an exception message).
Do you, by any chance, have the debug setting "Just My Code" set? This might be causing you some grief.
Also, you might want to poke around with the Exceptions dialog. Have a look at http://msdn.microsoft.com/en-us/library/038tzxdw.aspx and reverse the logic, so that you don't break on user-unhandled exceptions.
Dave
snkar
Member
5 Points
5 Posts
Re: Throwing exception results in Debugger Break
Mar 14, 2012 05:39 AM|LINK
You were absolutely correct about the Just My Code setting. That is what was creating the issue.Thanks a lot for pointing me in the right direction. There are so many features to Visual Studio that are not so specifically known till you encounter a need to explicitly change it. Is there any resource available which can readily list at least the important features/tips/tricks of the IDE?
But I am not sure on when should this "Just My Code" setting be useful. I could think of only the situation when probably I am trying to debug my code but do not want to step into an external library which I might be using.
And this code was actually just for fun