Try Catch...need my brain rattled.

D

darrel

Trying to get back into .net again after being out of it for a while.

I'm trying to figure out the proper way to handle multiple events via a try
catch.

What I'm confused about is the proper method to handle, say, 3 separate
events: A, B, and C. I only want all 3 to execute if all 3 can successfully
execute.

Is that what a try-catch is for, or is a try/catch mainly for individual
events?

Is there a best-practice way to handle checking for 3 separate events and
only executing all 3 only if they all can execute without error?

-Darrel
 
H

Harry Simpson

You could have three actions within a try catch even with multiple catches
If any fail it would go directly to the catch and you could pop outta the
procedure at that point.
 
S

Scott M.

Try...Catch doesn't handle events, it handles exceptions raised by your
code.

If you want to write a common event handler (one procedure that is called
when multiple events fire), you need to register the procedures as event
handlers. In VB .NET, it's easy: just extend the "Handles" clause of an
existing event handler with a comma and list the other events you wish to
handle:

Public Sub multiEventHandler(ByVal sender As System.Object, e As EventArgs)
_
Handles Button1.Click, Button2.Click, Button3.Click

In C#, it's a bit more involved. You'd need to go to the Page's Init event
and register the procedures as handlers for a particular events:

button1.Click += button_click()
button2.Click += button_click()
button3.Click += button_click()

-Scott
 
D

darrel

You could have three actions within a try catch even with multiple catches
If any fail it would go directly to the catch and you could pop outta the
procedure at that point.

Right, but that would merely check them sequentially, correct? for example:

Try
A()
B()
C()
catch
end try

A and B could execute even if C fails?

-Darrel
 
D

darrel

Try...Catch doesn't handle events, it handles exceptions raised by your

Sorry...used a bad word there. I wasn't referring to event handlers but
rather just executing code. Say I had 3 functions that do something. I only
want all 3 to do what they do only if the other 2 can also do what they do.

The more I think about this, the more I realize it's not any sort of
automated thing and that it's just me checking for each individually,
try/catch each one, and then rollback whatever I did if anyone of the 3
fails.

-Darrel
 
S

Scott M.

You could just simply have each procedure return a boolean indicating
success. That way you'll know if the procedure was successful before
invoking the next one.
 
S

Scott M.

Do you have control over the source code of A, B, and C?

If you do, I think you are thinking about this scenario incorrectly. If you
are the one coding A, B, and C, you would have those procedures doing the
try...catch and if an exception is encountered in those procedures, catch it
and return False from the method. Otherwise return true.

Then your calling procedure can just do:

If A then
If B then
C
End If
End If

If this is a matter of rolling back if you can only get so far, you should
consider using Transactions to help automate that.
 
D

darrel

Then your calling procedure can just do:
If A then
If B then
C
End If
End If

That makes sense, but, again, that seems sequential. What if C fails? I have
still done A and B, right?
If this is a matter of rolling back if you can only get so far, you should
consider using Transactions to help automate that.

Yes. I agree. And it sounds like you are validating this. Let SQL handle
what it does best and not try to do it all in the .net code. ;o)

-Darrel
 
A

Anthony Jones

darrel said:
That makes sense, but, again, that seems sequential. What if C fails? I have
still done A and B, right?


Yes. I agree. And it sounds like you are validating this. Let SQL handle
what it does best and not try to do it all in the .net code. ;o)

What do these functions actually do?

It sounds like what you are after is a transactional system whereby the
effects of A, B and C must only be commited if all three are successful.
If the effects are changes to a database (or multiple databases that support
DTC) then its simple enough to enlist all the DB operations into a
containing transaction.

OTH, if the effects are on other sorts of resources you will need to find a
way to rollback changes done so far if a later operation fails.

Pratically then what you want is possible if you can enlist all the
operations into a containing transaction.
 
G

Göran Andersson

darrel said:
Trying to get back into .net again after being out of it for a while.

I'm trying to figure out the proper way to handle multiple events via a
try catch.

What I'm confused about is the proper method to handle, say, 3 separate
events: A, B, and C. I only want all 3 to execute if all 3 can
successfully execute.

Is that what a try-catch is for, or is a try/catch mainly for individual
events?

Is there a best-practice way to handle checking for 3 separate events
and only executing all 3 only if they all can execute without error?

-Darrel

You can't do that using merely a try...catch. You either need a way to
tell if a method will be able to execute before actually executing it,
or a way to rollback the method.

Either:

if (ACanExecute() && BCanExecute() && CCanExecute()) {
A();
B();
C();
}

or:

try {
A();
try {
B();
try {
C();
} catch (SomeException ex) {
RollBackA();
RollBackB();
RollBackC();
}
} catch (SomeException ex) {
RollBackB();
RollBackA();
}
} catch (SomeException ex) {
RollBackA();
}

or:

if (A()) {
if (B()) {
if (!C()) {
RollBackC();
RollBackB();
RollBackA();
}
} else {
RollBackB();
RollBackA();
}
} else {
RollBackA();
}
 
D

darrel

You can't do that using merely a try...catch. You either need a way to
tell if a method will be able to execute before actually executing it,
or a way to rollback the method.

Thanks! That confirms my line of thinking!

_Darrel
 
D

darrel

What do these functions actually do?

Well my real world example was a file system update + a DB update. In
reality, the try-catch will be fine, as if the DB fails then the page likely
isn't loading at all, so I can mainly just check for the file system
function in the try-catch. If it fails, the DB update doesn't happen.

But then that got be thinking hypothetically of exactly what you decribe:
It sounds like what you are after is a transactional system whereby the
effects of A, B and C must only be commited if all three are successful.

Indeed, at that point, hopefully I'd be doing everything within SQL and
leverage transactions.

Otherwise, as Goran states, I write a bunch of code. ;o)

-Darrel
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top