Trapping exceptions encountered in user controls

Trapping exceptions encountered in user controls

am 17.01.2008 13:30:52 von 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

RE: Trapping exceptions encountered in user controls

am 17.01.2008 14:00:02 von 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

"Yash" wrote:

> 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
>

Re: Trapping exceptions encountered in user controls

am 17.01.2008 15:49:43 von 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 ???)...
--
Patrice


"Yash" a écrit dans le message de news:
eb94425e-79a2-4dc2-8081-c0347eeb7ac2@s19g2000prg.googlegroup s.com...
> 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

Re: Trapping exceptions encountered in user controls

am 17.01.2008 16:03:15 von 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?

Re: Trapping exceptions encountered in user controls

am 17.01.2008 17:04:38 von 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

--
Patrice



"Yash" a écrit dans le message de news:
3b2db41c-5364-4869-a575-dfe46f8a85a7@i12g2000prf.googlegroup s.com...
> 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?