Throwing Exception in a composite control

  • Thread starter Thread starter KJ
  • Start date Start date
K

KJ

This is kind of hard to explain but I have a [composite control] The
controls are created with CreateChildControls(). Now let say I click a
button and an error occurs in the control. If I throw it up it goes
back to the web form. where do I catch the exception at?


Example
Webform

Composite Control

but_click

Throw e as exception



Then the exception goes back up the chain until it gets to the
webform. Can I catch the exception in any event on the webform?
Because after it get pass the control the chain is broken.

Or do I have to pass it as a parameter like RaiseEvent Click(Me, e,
exception)?

I hope this is clear. If anyone know of a good resource for throwing
exception from composite controls let me know.
 
Does your control know how to handle the exception? Then it should catch the
exception in the catch block of a try-catch statement. The exception should
be caught where it can be handled.

If your control doesn't know what to do with the exception, then leave it
alone. It's up to the person who knows how to handle the exception to have
the exception handler.

BTW, this has nothing at all to do with composite controls. This is a basic
matter of one method calling another,. which calls another - which raises an
exception. Exception processing does nothing special with the control
hierarchy.
 
Why do you think you want to catch it in the webform? What do you expect to
do about such an exception? Are you concerned about some one particular
exception, or just exceptions in general?

Is your control explicitly throwing a particular exception? If so, then
please stop doing that. Exceptions are for exceptional circumstances.
They're not a general non-local goto.

You say it's not that easy with composite controls. You're dead wrong. Your
situation is in no way specific to controls, much less to composite
controls. This is nothing more than a chain of subroutine calls. I strongly
recommend that you stop asking your questions about controls and instead
just ask them about subroutines calling subroutines which call subroutines.

Has this exception ever occurred? In that case, one really big hint will be
the stack trace. Look at the trace and pretend you've never even _heard_
about controls!

If necessary, post the stack trace and I'll become less general. But I'm
concerned that if I do that, you won't learn the real lesson - that this has
nothing whatsoever to do with controls.
 
Kyle,

First, like I said, you should abstract the problem away from "controls" and
"web forms" to "components" and "component consumers".

However, you were given a good suggestion, one which would work whether this
were a "control" issue or a "component" issue. Your control should define an
Error event with the exception as a parameter. btnSearchClick (NOT "Search")
should catch any exceptions and should raise them as Error events to whoever
cares about Error events. Possibly even your Web Form.
 
Can you explain what you mean by 'error events'? I'm fairly new to this.
Thanks for your help.
 
I meant an event called "Error" which is raised to indicate that an error
has occurred. Such an event can be handled by any number of pieces of code
which care whether an error has occurred.
 
Back
Top