Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 15th, 2005, 10:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default Exception handling hurts my head

Hi All,
Im having a little trouble getting my head around handling exceptions in a process that has several stages.
If anyone can describe the correct apporach at a high level that would be very much appreciated.
Basically, I need to respond to a request for information via a web service.
If any of the stages in the processing of the request fail the message still needs to be sent back but with an appropriate error code/message from the interface system.

Do I need to nest my try catch blocks to ensure that code is not excecuted after an exception has been thrown?


So if I trap an database error and then go to excecute the more code that may rely on variables set from previous database call, I would expect them to fail too.

But that exception will replace the real error in the response message.

Does throwing a new exception of whatever type I decide exit the method?






======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old September 16th, 2005, 04:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To my knowledge there are several approaches, e.g. can't you just put the entire code in the block? You say that you want to return with an error message if anything fails.

You can also re-throw the exception from within the catch. This will enable you to set the proper error message etc. and make sure that it will terminate the method imidiately.

You can use custom exceptions to catch the exact exception which you have discovered.

Finally, if you still want to execute code after the catching - closing DB etc - then you can just use the finally construction.

Hope it helps, Jacob.
 
Old September 16th, 2005, 04:20 AM
Registered User
 
Join Date: Sep 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to freddywong2
Default

:) Beacause I begin to study C# , and I think so what you say!

wang
 
Old September 21st, 2005, 03:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks for your iput guys, I think I'll have to look into my requirments again.
See some of the records may fail and some may pass.
So in the loop I need to report error but not stop the processing.
So I guess I need a couple of custom exceptions to deal with this.
Thanks again

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================





Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception Handling NewTitle2007 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 6 August 8th, 2007 04:03 AM
exception handling jay schumacher VB.NET 2002/2003 Basics 1 April 14th, 2006 08:40 AM
Exception handling aldwinenriquez General .NET 0 August 17th, 2005 06:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.