Exception in the User Control

P

Prince Mathew

Hi All,

I have a requirement. I am throwing an exception from the Page_Load of my
user control I want
to catch this in my container page. Is this possible?
The Page_Load of user control is executed after the Page_Load of the page.
So we cannot catch this in the Page_Load of the page. So my question where
in the page i will catch the exception thrown form the user control.

I don't want any event to be raised from the user control.

PLEASE HELP ME.

Thanks

Prince
 
T

Teemu Keiski

You can have an error handler at the Page level (Page_Error) which catches
the error and does some processing, but it might have other effects
(unwanted ones) on your app. But try it out.

Personally if it is known that error is raised I would either:

-Try to handle it in the UC itself (own error handler there) and not let
Page do anything with it

or

-if Page needs know about it but doesn't need to do more, indeed raise an
event from the UC

And please note that raising exceptions shouldn't be normal logic, but they
would really need to be used in exceptional situations. Maybe you could
redesign it so that Page invokes the action in UC by calling UC's method,
when it could also handle possible exceptions or return data coming from the
method call (which would indicate error case)

But all these depends on what you need to do withing the Page 8there could
be other alternatives as well). Write the error data for user or something?
 
P

Prince Mathew

Hi,

"Write the error data for user or something?" Thats what i want. But with
out handling the error event or raising an event from the UC can i catch
that exception from the container Page.

Thanks
Prince
 
T

Teemu Keiski

But with out handling the error event or raising an event from the UC can
i catch that exception from the container Page.

Nope, because there isn't anything in "middle" that would initiate that
logic from page, or where you could plug your own code (as long as you keep
it withing Load event of the UC). As I said, it would mean a change so that
Page is the responsible for initiating the action (calling the method in UC
which can cause the error) if you want to have try...catch block to handle
it

Exception in Page_Error comes as general Exception type. If you'd want it
typed, you'd need to check the type first and cast to it (if there's chance
for expections of multiple type). Page_Error runs for all unhandled
exceptions.

Maybe you could get around it by assigning the Label or what the error msg
should be written to, as a proprrty from page to the UC, so UC could set it
itself. Or its ID. Anyways, its your call. The responibility goes into two
possible ways here, either Page does the writing or then UC does it. If Page
does it, it needs to get to the exception somehow and these are the means.

-
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU

..
 
P

Prince Mathew

Thanks
Teemu

Teemu Keiski said:
Nope, because there isn't anything in "middle" that would initiate that
logic from page, or where you could plug your own code (as long as you
keep it withing Load event of the UC). As I said, it would mean a change
so that Page is the responsible for initiating the action (calling the
method in UC which can cause the error) if you want to have try...catch
block to handle it

Exception in Page_Error comes as general Exception type. If you'd want it
typed, you'd need to check the type first and cast to it (if there's
chance for expections of multiple type). Page_Error runs for all unhandled
exceptions.

Maybe you could get around it by assigning the Label or what the error msg
should be written to, as a proprrty from page to the UC, so UC could set
it itself. Or its ID. Anyways, its your call. The responibility goes into
two possible ways here, either Page does the writing or then UC does it.
If Page does it, it needs to get to the exception somehow and these are
the means.

-
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU

.
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,148
Latest member
ElizbethDa
Top