Catching exceptions?

You don't have to catch every possible exception around every line of code.

You don't have to catch every possible exception around every line of code. Instead, only catch when you have context and you are prepared to handle and take appropriate action. Otherwise, just let it bubble up.

Catching exceptions (especially deep in the stack in common functions where you don't have enough context to handle) can be problematic. It sometimes leads to swallowing and not appropriately handling. Handle exceptions when you have context.

For that FileInfo exception it depends how deep in that stack it is. If it's in a library where it can be reached via multiple paths and scenarios, you shouldn't handle, what are you going to do at that point? If that's also the case and it bubble all the way out through a deep stack to a GUI performing an operation where you want to handle it, what are you going to catch?

Every possible exception of every code path on it's execution path? And, as code changes, you're going to re-evaluate all paths? In a shallow code path (file info dialog that calls that method) it's an easy call but even then you're not going to handle it differently - you're going to display an error dialog or message in panel.

Exception types offer the ability to handle differently. In both of those case, you handle exception so I say bah hum bug to that guideline. What is very clear is you should not throw type Exception.

That short circuits the ability to handle differently. Another related post: Trying to understand exceptions in C.

I read that line of thinking a lot, and I don't quite agree with it. Consider two scenarios: (1) System. ArgumentException occurs while trying to open a file at the start of a LoadDocument method, and the effect is that the document doesn't open but system state is otherwise undisturbed; (2) System.

ArgumentException occurs at some other point in LoadDocument, as a consequence of e.g. Improper cross-thread operation corrupting program state. If the former ArgumentException bubbles up, how can the calling code distinguish it from the latter? – supercat Dec 15 at 4:41 I would suggest that the proper course of action would be to catch and wrap system exceptions in a manner that indicates something about the overall system state.

Otherwise, the only way to allow recovery of what should be recoverable exceptions would be to use Pokemon exception handling. – supercat Dec 15 at 4:42.

Also, you can test the path first with File. Exists(path) or Directory. Exists(path).

1 avoiding exceptions for flow control is always a great thing. – bryanmac Dec 1 at 6:32 1 Just remember that, however unlikely, there is a race condition here. It's possible that the file or directory could disappear between the time you check for it and the time you actually try to use it.

– Marty Dill Dec 14 at 23:55.

You could do something like this: try { var fileInfo = new FileInfo(path); } catch (System.IO. PathTooLongException ex) { handleError(ex); } catch (System. ArgumentException ex) { handleError(ex); } catch (System.

UnauthorizedAccessException ex) { } catch (System. SecurityException ex) { handleError(ex); } public void handleError(Exception ex) { //do some stuff }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions