473,503 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

terminating multiple try/catches

Not quite sure how to word this.

I have a button click function that calls two different subs. One updates a
database, the other writes to a file.

Both of the subs have a try/catch within them. I'd like to have both of them
execute if they can, but if one can't, then have neither execute. Is there a
way to do this that isn't too complex? I assume I'd need to first call them
and have then run-through on a 'trial run' and if succeed, return a value.
If both are returned as 'doable' then call them both again for the actual
execution.

In this particular case, one is dependant on the other but not vice versa,
so I can simply call the first one, and then call the second one if the
first one executes. But, in the future, I could see wanting two separate
functions both being dependant on each other. Or is that bad design?

-Darrel
Nov 19 '05 #1
6 1053
why cant one simpley execute after the other, write the file first - if it
succeeds then write to DB, if that fails delete file in catch.

You could always trap your write event failure code in your catch for file
write and evaluate it in your db execute, with clean up in the finally
clause of DB execute.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"darrel" <no*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Not quite sure how to word this.

I have a button click function that calls two different subs. One updates a database, the other writes to a file.

Both of the subs have a try/catch within them. I'd like to have both of them execute if they can, but if one can't, then have neither execute. Is there a way to do this that isn't too complex? I assume I'd need to first call them and have then run-through on a 'trial run' and if succeed, return a value.
If both are returned as 'doable' then call them both again for the actual
execution.

In this particular case, one is dependant on the other but not vice versa,
so I can simply call the first one, and then call the second one if the
first one executes. But, in the future, I could see wanting two separate
functions both being dependant on each other. Or is that bad design?

-Darrel

Nov 19 '05 #2
> why cant one simpley execute after the other, write the file first - if it
succeeds then write to DB, if that fails delete file in catch.


It can. Like I said, in this case, it's not a big deal, but I can see a need
where I may need both to execute and not always in the same order. (I admit,
I'm speaking in hyptheticals here.)

-Darrel
Nov 19 '05 #3
Well as they are technically serpeate subs, you would need a way of marking
them as having failed. With DB you should ideally be using transactions and
rollbacks, so your finally or catch event could evaluate success and commit
or not - it really depends on many different factors but hypothetically its
achieveable with a simple static/session variables acting as success/failure
markers or evqluating getlasterror which you could throw and evaluate on
entering any of the subs.

http://msdn.microsoft.com/library/de...errortopic.asp

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"darrel" <no*****@hotmail.com> wrote in message
news:uJ**************@TK2MSFTNGP14.phx.gbl...
why cant one simpley execute after the other, write the file first - if it succeeds then write to DB, if that fails delete file in catch.
It can. Like I said, in this case, it's not a big deal, but I can see a

need where I may need both to execute and not always in the same order. (I admit, I'm speaking in hyptheticals here.)

-Darrel

Nov 19 '05 #4
Thanks, John.

Again, this is more just for discussion than anything specific. I'll take a
look at that link and tuck it away in the back of my head if/when I ever
need it. ;o)

-Darrel

"John Timney (ASP.NET MVP)" <ti*****@despammed.com> wrote in message
news:OI**************@TK2MSFTNGP15.phx.gbl...
Well as they are technically serpeate subs, you would need a way of marking them as having failed. With DB you should ideally be using transactions and rollbacks, so your finally or catch event could evaluate success and commit or not - it really depends on many different factors but hypothetically its achieveable with a simple static/session variables acting as success/failure markers or evqluating getlasterror which you could throw and evaluate on
entering any of the subs.

http://msdn.microsoft.com/library/de...errortopic.asp
--

Nov 19 '05 #5
darrel wrote:
I have a button click function that calls two different subs. One updates a database, the other writes to a file.

Both of the subs have a try/catch within them. I'd like to have both of them execute if they can, but if one can't, then have neither execute. Is there a way to do this that isn't too complex? I assume I'd need to first call them and have then run-through on a 'trial run' and if succeed, return a value. If both are returned as 'doable' then call them both again for the actual execution.


In this specific case, could you wrap the Database operation in a
transaction, and have the first sub return a success code?

if (firstthing())
{
secondthing();
}

bool firstthing()
{
using (trans = new SqlTransaction()...)
{
try
{
insertOrWhatever();
trans.Commit();
}
catch
{
return false;
}
}

return true;
}
This would seem to work. If your database manipulation fails, your
transaction will rollback and you'll know not to try writing to the
filesystem.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #6
aren't you getting that functionality? if you remove the trycatch in the
first sub, delegate the trycatch to the caller, and then if sub1 fails, sub2
never runs.

thats the KISS answer...

--
Eric Newton
eric.at.ensoft-software.com
www.ensoft-software.com
C#/ASP.net Solutions developer

"darrel" <no*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Not quite sure how to word this.

I have a button click function that calls two different subs. One updates
a
database, the other writes to a file.

Both of the subs have a try/catch within them. I'd like to have both of
them
execute if they can, but if one can't, then have neither execute. Is there
a
way to do this that isn't too complex? I assume I'd need to first call
them
and have then run-through on a 'trial run' and if succeed, return a value.
If both are returned as 'doable' then call them both again for the actual
execution.

In this particular case, one is dependant on the other but not vice versa,
so I can simply call the first one, and then call the second one if the
first one executes. But, in the future, I could see wanting two separate
functions both being dependant on each other. Or is that bad design?

-Darrel

Nov 19 '05 #7

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

Similar topics

4
1670
by: anonymous | last post by:
When I use the schema collection to apply many schemas to one XML instance document, I get an error if I do not qualify every element with the appropriate namespace. Both the W3C site and this...
23
7538
by: Adam Clauss | last post by:
I have a C# Windows Service running as the NetworkService account because it needs to access a network share. As part of the service's initialization, I want the service to terminate, if an...
3
2978
by: zacks | last post by:
The help isn't clear on this. If I have multiple catches and the first one catches the particular exception that was thrown, does it check the rest of the catches in the try/catch? For example,...
4
1247
by: roman | last post by:
Hi, I have a form which validates data before submitting, it catches the error, but still submits and can't seem to figure out why. Can someone look at my code and give me some ideas how to...
5
4936
by: paul_zaoldyeck | last post by:
does anyone know how to validate an xml file against multiple defined schema? can you show me some examples? i'm making here an xml reader.. thank you
0
7274
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
7323
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...
1
6984
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
7453
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...
0
5576
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,...
1
5005
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...
0
4670
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...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
732
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.