开发者

How to generate unhandled exceptions in ASP.NET Web App?

I put some global error handling in place, but am having problems testing it by causing unhandled exceptions.

Here's all I can think of at the moment. Please feel free to add to this list with more ways to trip unhandled exceptions.

  1. Dangerous form data - Entering characters such as < and > in a text box and trying to submit
  2. Put invalid values in a URL parameter - eg if you page is www.test.com/home?testid=XXX where XXX is the form number/identity # you are trying to pull up, put a number that doesn't exist in the URL and hit enter.

I'm sure I could c开发者_JAVA技巧hange some stored procedures or otherwise mess with my data access components but I'd rather not have to change any code anywhere...I want to be able to generate these exceptions on the front end, like a user would.

Let me know what ideas you have, or if you all need any other info.


One possible way to do this would be to use querystring parameters that you append to your URL to force an exception to be thrown, probably restricted to debug builds or via a setting in web.config so that end users who learn about your parameters can't play around.

Something like

http://mypage.aspx?...&ThrowException=true&ComponentName=Whatever...

You'll have to design the syntax, then manually append the parameters to URLs in the address bar. Then you can add calls to a static helper method something like the following at strategic places in your code where you want to test exception handling (e.g. the data access layer)

[Conditional("Debug")]
static void SimulateError(string componentName)
{
    if(Request.QueryString["ComponentName"] == componentName)
    {
        throw ...;
    }
}


Use

throw new Exception("this is my exception");

somewhere in the code that gets executed upon calling the requested page


You aren't going to be able to test all your exceptions from the end user environemnt because that's not where most of your unhandled exceptions should be coming from.

This question might be useful to you as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜