473,394 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Try Catch...need my brain rattled.

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

Jul 22 '08 #1
11 1460
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.
"darrel" <no*****@notreal.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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

Jul 22 '08 #2
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

"darrel" <no*****@notreal.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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

Jul 22 '08 #3
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

Jul 22 '08 #4
Try...Catch doesn't handle events, it handles exceptions raised by your
code.
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

Jul 22 '08 #5
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.
"darrel" <no*****@notreal.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>Try...Catch doesn't handle events, it handles exceptions raised by your
code.

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

Jul 22 '08 #6
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.

"darrel" <no*****@notreal.comwrote in message
news:uk**************@TK2MSFTNGP06.phx.gbl...
>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

Jul 22 '08 #7
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
Jul 22 '08 #8
"darrel" <no*****@notreal.comwrote in message
news:O3**************@TK2MSFTNGP06.phx.gbl...
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)
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.
--
Anthony Jones - MVP ASP/ASP.NET
Jul 23 '08 #9
darrel wrote:
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();
}

--
Göran Andersson
_____
http://www.guffa.com
Jul 23 '08 #10
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
Jul 23 '08 #11
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
Jul 23 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rahul | last post by:
Hi, I have a little program as follows : =================== STARTS HERE ================ #include <stdio.h> void f (unsigned long); int main() {
23
by: Adam | last post by:
I am coding a microkernel based off of Tanebaum's theroy. For Isis to be extensible, fast, and secure, it has been decided it will be a microkernel. Not in the old Mach sense of the word, but in...
3
by: RC | last post by:
Dear Dudes, I post this in multiple groups for opening brain storm. Sometime I need to query the data from database server then display them into user's browser in HTML <table>. But if the...
28
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
7
by: Lloyd Sheen | last post by:
I noticed a thread a few days ago about the use of Try.. Catch versus testing variables etc and had in my mind to test exactly what the impact was. Its a mind boggler. I created a small windows...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.