Trapping exceptions encountered in user controls

Y

Yash

Hi,

I have create a user control which has a dropdown. The user control
internaly handles the Selected_Index_Changed event of the dropdown. If
there is an error/exception in handling it, I would like the
containing page to know of it. How can I make this possible? The
containing page does not know of the dropdown.

Thanks,
Yash
 
J

JIGNESH

To give you an idea therotically.
1. Declare event in your usercontrol.
2. From your page trap the event UserControl += new MyEvent( ...... )
3. OnException inside UserControl Raise the Event
4. You Page event handdler will receive the event and execute your function.

if you find difficult then i shall spend sometime to code and post it here.

Regards
JIGNESH
 
P

Patrice

It doesn't matter. The exception bubbles and will be handled at the parent
level inside the hosting application (make sure you don't hide the exception
in your user control ???)...
 
Y

Yash

But where will the try and catch be in the containg page to handle the
bubbled exception?

Raising an OnError event may work. But is there another way?
 
P

Patrice

Either in the Page_Error event or in the global exception event handler (my
personal preference)...

The general idea is that exceptions are propagated automatically to callers.
So the first step could be to check your first assumption. This is not
because the dropdown is not an object known from the parent page that
exceptions thrown there are not propagated back to the caller (it just uses
the call stack to pass the exception to calling code).

With this in mind the general principle would be to have a global error
handler at the highest possible level so that you knows that something goes
wrong and that you can fix the coding error. Then you can place a local
exception handler at choosen places :
- either because your code or the user could do something to solve the
particular exception you are trapping at this place (and in this case you
don't let this particular one to propagate higher)
- or to do some cleanup on unmanaged resources (but in this case you still
let the exception goes higher as once resources are released you still want
to know that your application has en error you should fix).

You should find more details areound :
http://msdn2.microsoft.com/en-us/library/6kzk0czb.aspx
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top