473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Try/Catch for Events

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?
Jun 27 '08 #1
3 1071
"randy.buchholz" <ra************@dads.state.tx.uswrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>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
}
}

Jun 27 '08 #2
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);
}
}
}

"Leon Mayne" <le**@rmvme.mvps.orgwrote in message
news:D7**********************************@microsof t.com...
"randy.buchholz" <ra************@dads.state.tx.uswrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>>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
}
}

Jun 27 '08 #3

"randy.buchholz" <ra************@dads.state.tx.uswrote in message
news:uv**************@TK2MSFTNGP04.phx.gbl...
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.

Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Martin Ortiz | last post by:
Which is best approach? Should Try + Catch be used to only deal with "catastrophic" events (like divide by zero, non-existant file, etc...etc...) Or should Try + Catch be used IN PLACE of...
11
by: Terry Olsen | last post by:
How can I catch a right-click on a DropDownMenuItem?
5
by: Simon Tamman {Uchiha Jax} | last post by:
Now this is bugging me. I just released software for a client and they have reported an unhandled stack overflow exception. My first concern is that the entirity of the UI and any threaded...
1
by: Paul Fi | last post by:
how do i catch windows explorer and windows media player events in a .NET application, is it possible, say i double clicked on a media file inside windows media player and i wanted that event to...
5
by: sleepinglord | last post by:
An example: I have a select input. I wanna catch those onclick events which is not a onchange events. How to implement it? And in general, there's some basic kinds of events, and I wanna catch...
2
by: gazelle04 | last post by:
I want to catch events between subforms whenever I changed data on the first form and click on the second form. I'm on the main form. The main form consists of two subforms. I want to catch the...
3
by: SimeonArgus | last post by:
I have a situation that I can't seem to work around. I have a database that lists the buttons that my application should generate. These buttons are displayed if certain conditions are met and all...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
11
by: darrel | last post by:
Trying to get back into .net again after being out of it for a while. I'm trying to figure out the proper way to handle multiple events via a try catch. What I'm confused about is the proper...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.