Click here to Skip to main content
15,906,624 members
Home / Discussions / C#
   

C#

 
AnswerRe: Where to use Try/Catch block? Pin
BobJanova13-Sep-11 22:40
BobJanova13-Sep-11 22:40 
GeneralRe: Where to use Try/Catch block? [modified] Pin
Adam_Dev13-Sep-11 23:01
Adam_Dev13-Sep-11 23:01 
GeneralRe: Where to use Try/Catch block? Pin
BobJanova14-Sep-11 2:09
BobJanova14-Sep-11 2:09 
AnswerRe: Where to use Try/Catch block? Pin
Pete O'Hanlon13-Sep-11 22:57
mvePete O'Hanlon13-Sep-11 22:57 
GeneralRe: Where to use Try/Catch block? [modified] Pin
Adam_Dev13-Sep-11 23:10
Adam_Dev13-Sep-11 23:10 
GeneralRe: Where to use Try/Catch block? Pin
Pete O'Hanlon13-Sep-11 23:50
mvePete O'Hanlon13-Sep-11 23:50 
GeneralRe: Where to use Try/Catch block? Pin
Adam_Dev14-Sep-11 0:26
Adam_Dev14-Sep-11 0:26 
AnswerRe: Where to use Try/Catch block? Pin
Luc Pattyn14-Sep-11 2:11
sitebuilderLuc Pattyn14-Sep-11 2:11 
You could have all your methods contain try-catch constructs, and return a success/failure result somehow (that is what the Win32 API has done for as long as Windows exists); the disadvantage is you have to provide all the code to do that, *and* your callers have to explicitly check each and every return value.

Or you could take full advantage of the Exception mechanism, which basically says: if something goes wrong, an Exception will be thrown, and some piece of code is bound to catch it, either in the method itself, or one of the callers higher up in the calling chain; if there are no catchers, then the app will exit. You get all this for free.

So yes, a single try-catch at the top level would allow you to log the exception and exit, although that would be an ungraceful exit (and an app I would not like to use). As others have said, use a local try-catch if and when your catch block is able to resolve the problem, and leave the exception as is if you can't resolve it at that level.

And yes, input checking and throwing explicitly on bad input values is the normal thing to do.

And yes, having a catch block that just contains throw is pointless; having a catch block that contains logging and throw normally results in multiple logging. What I sometimes do is catch an exception, and throw a different exception (a "smarter" one, one that fits the usage domain of the method) while putting the original exception in the new one's InnerException. So a "File Not Found" situation deep inside a simulator program may end up as a "Simulation failed" exception, where the tree of inner exceptions ends up explaining why or how it failed.

FWIW: I would recommend you study the topic in more detail in a book on .NET in general, or on C#; an introductory book should contain a number of examples of all of the above. And I do mean a physical book, not an eBook or some web course.

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

GeneralRe: Where to use Try/Catch block? Pin
BobJanova14-Sep-11 2:22
BobJanova14-Sep-11 2:22 
GeneralRe: Where to use Try/Catch block? Pin
Adam_Dev14-Sep-11 2:33
Adam_Dev14-Sep-11 2:33 
AnswerRe: Where to use Try/Catch block? Pin
Luc Pattyn14-Sep-11 2:51
sitebuilderLuc Pattyn14-Sep-11 2:51 
JokeArrayList Pin
Subodh Kumar Jain13-Sep-11 20:38
Subodh Kumar Jain13-Sep-11 20:38 
GeneralRe: ArrayList Pin
Wayne Gaylard13-Sep-11 21:04
professionalWayne Gaylard13-Sep-11 21:04 
GeneralRe: ArrayList Pin
PIEBALDconsult14-Sep-11 2:27
mvePIEBALDconsult14-Sep-11 2:27 
GeneralRe: ArrayList Pin
Pete O'Hanlon14-Sep-11 3:51
mvePete O'Hanlon14-Sep-11 3:51 
QuestionError in Custom IIS MMC SnapIn Page ...... Pin
Shazz_Shivang200413-Sep-11 20:23
Shazz_Shivang200413-Sep-11 20:23 
QuestionWho to write HTML code using Csharp Pin
amit_ghosh1813-Sep-11 8:20
amit_ghosh1813-Sep-11 8:20 
AnswerRe: Who to write HTML code using Csharp PinPopular
Eddy Vluggen13-Sep-11 8:33
professionalEddy Vluggen13-Sep-11 8:33 
AnswerRe: Who to write HTML code using Csharp Pin
Matt Meyer13-Sep-11 8:44
Matt Meyer13-Sep-11 8:44 
AnswerRe: Who to write HTML code using Csharp Pin
Ian Shlasko13-Sep-11 8:52
Ian Shlasko13-Sep-11 8:52 
AnswerRe: Who to write HTML code using Csharp Pin
PIEBALDconsult13-Sep-11 10:30
mvePIEBALDconsult13-Sep-11 10:30 
AnswerRe: Who to write HTML code using Csharp Pin
BobJanova13-Sep-11 22:49
BobJanova13-Sep-11 22:49 
Questionasp.net 2010 toolbox items Pin
classy_dog13-Sep-11 3:59
classy_dog13-Sep-11 3:59 
AnswerRe: asp.net 2010 toolbox items Pin
BobJanova13-Sep-11 4:06
BobJanova13-Sep-11 4:06 
AnswerRe: asp.net 2010 toolbox items Pin
fjdiewornncalwe13-Sep-11 5:43
professionalfjdiewornncalwe13-Sep-11 5:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.