Reputation: 23
If the user waits for 5 minutes, my form display the following message when they click on the "valid" button
Server was unable to process request. ---> Original error message: Database problem while fetch entity failed. ---> [IBM U2][UODOTNET - UNICLIENT][ErrorCode=45002] Cannot perform operation on a null recordID
So I want use the try and catch keywords,
but I have an error mesage whit this code :
try
{
SetPageState();
Session["NewStep"] = "NewStepConfirm";
Response.Redirect(stepone.aspx");
}
catch (Exception ex)
{
if (ex.ErrorCode == 45002)
{
ErrorLabel.Text = "We are very sorry but don't wait for long time";
}
else
{
ErrorLabel.Text = ex.Message;
}
ErrorLabel.Visible = true;
}
here is a message error
Compiler Error Message: CS1061: 'System.Exception' does not contain a definition for 'ErrorCode' and no extension method 'ErrorCode' accepting a first argument of type 'System.Exception' could be found (are you missing a using directive or an assembly reference?)
Source Error:
if (ex.ErrorCode == 45002)
How I can use try and catch keywords
thanks
Upvotes: 0
Views: 1362
Reputation: 36082
Remove the test for ErrorCode, set a breakpoint in the catch block, run your app in the debugger and reproduce the failure.
When the debugger stops at the breakpoint, inspect the exception object. What is it's type? What fields does it have? It's possible that this exception object has the ErrorCode you're after. If so, you're done. Edit your catch clause to trap that specific exception type.
However, the structure of the error message (the "-->" and "Original Message") suggests to me that this is probably a wrapped exception - exception A thrown by core code is caught by intermediate code, which throws exception B. Hopefully B will retain a reference to the original exception A in the InnerException property. Check the InnerException property of your exception object, and the InnerException property of that object, and so on to follow the chain to find the originating exception, since that is the most likely to have the data you're after.
The error code you see in the text may not be a formal property of any exception object, it may just be data embedded in the string.
If the data you are after is buried in an InnerException, you can't catch that. You have to catch the type of the outermost exception (the type of the ex variable when the debugger stops at your breakpoint) and then dig around in the InnerException chain to pull out the data you're after.
Upvotes: 2
Reputation: 74267
You need to catch the specific type of exception that's being thrown.
It is considered bad practice (or at least, a last-ditch effort) to catch the base exception class. If you catch the specific exception type, you'll have access to the properties you're looking for.
try
{
DoSomethingThatThrowsACustomWidgetException() ;
}
catch (CustomWidgetException e )
{
int errorCode = e.ErrorCode ;
...
}
If you catch the base Exception class, you'll need to downcast it to its type:
try
{
DoSomethingThatThrowsACustomWidgetException() ;
}
catch ( Exception e )
{
CustomWidgetException cwe = e as CustomWidgetException ;
if ( cwe == null ) throw ;
int errorCode = cwe.ErrorCode ;
...
}
Easy!
Upvotes: 1