Try/Catch for Events

R

randy.buchholz

I am trying to capture event exceptions using try/catch.
I can get all exceptions with:

protected void dv_PR_ItemInserted(object sender,
DetailsViewInsertedEventArgs e)
{
if (e.Exception != null)
{
lbl_Errors.Text = e.Exception.Message.ToString();
e.ExceptionHandled = true;
}
else
{
Response.Redirect("~/PR/PR_Item.aspx?PR=" + tb_tPR.Text);
}
}

but then have to parse and switch() to get a meaningful message to the user.
Is there a way to use a try/catch in this situation so I can handle the
different exceptions individually?
 
L

Leon Mayne

randy.buchholz said:
I am trying to capture event exceptions using try/catch.
I can get all exceptions with:

protected void dv_PR_ItemInserted(object sender,
DetailsViewInsertedEventArgs e)
{
if (e.Exception != null)
{
lbl_Errors.Text = e.Exception.Message.ToString();
e.ExceptionHandled = true;
}
else
{
Response.Redirect("~/PR/PR_Item.aspx?PR=" + tb_tPR.Text);
}
}

but then have to parse and switch() to get a meaningful message to the
user.
Is there a way to use a try/catch in this situation so I can handle the
different exceptions individually?

I guess you could rethrow e.Exception in a try block and catch that, but it
may wipe the stack trace and change the exception type (haven't tried it).
Something like:

if (e.Exception != null)
{
try
{
throw e.Exception;
}
catch (StackOverflowException ex)
{
// Do stuff
}
catch (Exception ex)
{
// Do other stuff
}
}
 
R

randy.buchholz

Yes, that works and preserves the exception. Thanks. I've been searching
MSDN for a list of exceptions (especially PK violation) any ideas on where I
can find a good list? Intellisence has some, but not the ones I'm looking
for.

protected void dv_PR_ItemInserted(object sender,
DetailsViewInsertedEventArgs e)
{
if (e.Exception != null)
{
try
{
throw e.Exception;
}
catch (SystemException ex)
{
lbl_Errors.Text = ex.Message.ToString();
}
catch (Exception ex)
{
lbl_Errors.Text = ex.Message.ToString();
}
finally
{
e.ExceptionHandled = true;
}
}
else
{
Response.Redirect("~/PR/PR_Item.aspx?PR=" + tb_tPR.Text);
}
}
}
 
L

Leon Mayne

randy.buchholz said:
Yes, that works and preserves the exception. Thanks. I've been searching
MSDN for a list of exceptions (especially PK violation) any ideas on where
I can find a good list? Intellisence has some, but not the ones I'm
looking for.

It depends on what you are expecting to be thrown. I don't think looking
through a list of exceptions will help much. You may be better off breaking
the system yourself and seeing what gets thrown by stepping into the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top