Click here to Skip to main content
16,020,565 members
Home / Discussions / C#
   

C#

 
AnswerRe: Try/Catch Opinions Pin
Luc Pattyn23-Nov-11 10:26
sitebuilderLuc Pattyn23-Nov-11 10:26 
GeneralRe: Try/Catch Opinions Pin
Pete O'Hanlon23-Nov-11 10:33
mvePete O'Hanlon23-Nov-11 10:33 
GeneralRe: Try/Catch Opinions Pin
Luc Pattyn23-Nov-11 10:56
sitebuilderLuc Pattyn23-Nov-11 10:56 
GeneralRe: Try/Catch Opinions Pin
Pete O'Hanlon23-Nov-11 11:04
mvePete O'Hanlon23-Nov-11 11:04 
GeneralRe: Try/Catch Opinions Pin
Luc Pattyn23-Nov-11 11:11
sitebuilderLuc Pattyn23-Nov-11 11:11 
GeneralRe: Try/Catch Opinions Pin
Eddy Vluggen23-Nov-11 9:27
professionalEddy Vluggen23-Nov-11 9:27 
GeneralRe: Try/Catch Opinions Pin
BillWoodruff24-Nov-11 6:56
professionalBillWoodruff24-Nov-11 6:56 
GeneralRe: Try/Catch Opinions Pin
Pete O'Hanlon30-Nov-11 5:33
mvePete O'Hanlon30-Nov-11 5:33 
Bill, sorry for the delay in replying. To answer your unspoken question, yes and no - the code was answering the premise that exception handling should not be relied upon when a simple check can solve the issue. So, what is the error we're coping with in this example? It's the fact that you can't write a file into a directory until that directory exists.

The other side here, is the fact as you've rightly pointed out, that this doesn't address all the cases that can be tested for. In my defence, I wrote this as a cut-down sample in the CP editor as an example only, but you are right, there are many other conditions that could be catered for. The point is really two-fold, if you create a utility API to handle the conditions that you can test for (rather than catch as an exception) then you have given yourself a real advantage when you come to write your production code. No, you won't get rid of exception handling altogether, but you won't be relying on it to cope with conditions that could easily have been tested for.

So, why do I advocate this? Well, exception handling is meant to indicate something that you couldn't reasonably predict and cope with. The mark of a good application is one that doesn't surprise users, so I'm either going to have to do lots of try/catch (and then cope with the failure before triggering the condition in the try/catch again), or I can put something in place to cope with the error before it occurs and make an intelligent decision about coping with the problem.

I hope this somewhat rambling post conveys some of what I was trying to get across.

Forgive your enemies - it messes with their heads

"Mind bleach! Send me mind bleach!" - Nagy Vilmos


My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility


GeneralRe: Try/Catch Opinions Pin
Eddy Vluggen23-Nov-11 7:51
professionalEddy Vluggen23-Nov-11 7:51 
GeneralRe: Try/Catch Opinions Pin
jschell25-Nov-11 9:03
jschell25-Nov-11 9:03 
AnswerRe: Try/Catch Opinions Pin
Eddy Vluggen25-Nov-11 10:08
professionalEddy Vluggen25-Nov-11 10:08 
GeneralRe: Try/Catch Opinions Pin
jschell25-Nov-11 12:36
jschell25-Nov-11 12:36 
GeneralRe: Try/Catch Opinions Pin
Ennis Ray Lynch, Jr.23-Nov-11 8:34
Ennis Ray Lynch, Jr.23-Nov-11 8:34 
GeneralRe: Try/Catch Opinions Pin
PIEBALDconsult23-Nov-11 8:37
mvePIEBALDconsult23-Nov-11 8:37 
GeneralRe: Try/Catch Opinions Pin
Luc Pattyn23-Nov-11 10:25
sitebuilderLuc Pattyn23-Nov-11 10:25 
GeneralRe: Try/Catch Opinions Pin
jschell25-Nov-11 9:01
jschell25-Nov-11 9:01 
GeneralRe: Try/Catch Opinions Pin
emardini27-Nov-11 16:38
emardini27-Nov-11 16:38 
QuestionManage Services On Another PC/Server Pin
Kevin Marois23-Nov-11 6:59
professionalKevin Marois23-Nov-11 6:59 
AnswerRe: Manage Services On Another PC/Server Pin
Matt U.23-Nov-11 9:26
Matt U.23-Nov-11 9:26 
GeneralRe: Manage Services On Another PC/Server Pin
Kevin Marois23-Nov-11 10:16
professionalKevin Marois23-Nov-11 10:16 
QuestionC# Find and Replace Pin
stefanere2k923-Nov-11 6:02
stefanere2k923-Nov-11 6:02 
AnswerRe: C# Find and Replace Pin
BobJanova23-Nov-11 6:16
BobJanova23-Nov-11 6:16 
AnswerRe: C# Find and Replace Pin
PIEBALDconsult23-Nov-11 7:26
mvePIEBALDconsult23-Nov-11 7:26 
SuggestionRe: C# Find and Replace Pin
Nevin Janzen23-Nov-11 7:30
Nevin Janzen23-Nov-11 7:30 
AnswerRe: C# Find and Replace Pin
BillWoodruff24-Nov-11 7:24
professionalBillWoodruff24-Nov-11 7:24 

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.