473,788 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1488
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*****@notrea l.comwrote in message
news:%2******** ********@TK2MSF TNGP04.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 multiEventHandl er(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*****@notrea l.comwrote in message
news:%2******** ********@TK2MSF TNGP04.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*****@notrea l.comwrote in message
news:%2******** ********@TK2MSF TNGP06.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*****@notrea l.comwrote in message
news:uk******** ******@TK2MSFTN GP06.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*****@notrea l.comwrote in message
news:O3******** ******@TK2MSFTN GP06.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

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

Similar topics

2
2138
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
2603
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 the size of the entire project. It is a microkernel, that will limit interprocess communication. But as Tanenbaum says, you learn nothing by reading theroy alone. The code should be self-explaintory. Another reason I choose the microkernel method...
3
1699
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 <table> is very LARGE, let's say 20 columns, hundreds rows. Usually you have header <th> tags on 1st (top) row and 1st (most left) column. For a such LARGE <table>. you really want:
28
3815
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
7
1266
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 app that executed two subroutines. The first simply created an object which does not implement an interface and attempts to assign that object to an variable of the interface type. This is done in a try ... catch block.
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5402
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.