Try..Catch through sub/functions

N

Nemisis

HI everyone,

I have a sub that executes a query, within the sub there is a try,
catch, finally block. lets call this subA.

I also have another sub (subB) that calls SubA, and another Sub (SubC)
that calls SubB.

SubC (with Try..Catch) > SubB (without Try..Catch) > SubA (with
Try..Catch)

SubC has a try..catch block and so does SubA. If SubA does encounter
an error, why does it not error up through the subs?? SubB does NOT
have a try..catch block

So i have to turn these subs into functions and return a 1 or 0,
depending on whether a error has occurred or not??

Sorry if this is a silly stupid question, i am still getting to grips
with .net 2.0
 
K

Kevin Spencer

A catch statement does just that. It catches an exception, preventing the
exception from stopping execution. So, if you have a method that calls
another method, and the second method has a try/catch block in it, if an
exception occurs in the second method, it does not get thrown, but caught.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
P

Patrice

Bascially an exception "bubbles" up by design (i.e. will be handled by the
nearest try/catch). If you catch it at a lower level you prevent bubbling.
In some cases you could rethrow the exception (if you want to perform
something where it fails but still have outer procedures do something in
case of a failure).
 
L

Laurent Bugnion

Hi,
Bascially an exception "bubbles" up by design (i.e. will be handled by the
nearest try/catch). If you catch it at a lower level you prevent bubbling.
In some cases you could rethrow the exception (if you want to perform
something where it fails but still have outer procedures do something in
case of a failure).

The main difference between standard "bubble" models and the exception
model is that in the standard "bubble" model, the bubbles are propagated
without the user having to do it explicitly. In the opposite, the user
has to specifiy if he wants to stop the bubbling. For exceptions,
however, the "catch" statement stops the exception from going further,
unless the user explicitly throws it again for the next level.

HTH,
Laurent
 
N

Nemisis

Thank you all for your replies, i figured it out afterwards. It was
because i wasnt rethrowing an exception after the child try..catch
caught the exemption.

This way is bubbles up to the parent sub/function, as you all said.

Thanks again everyone
 

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

Top