Very simple really - is there a way to check and make a list of all exceptions that a method might throw? I have used try/catch but I want to make sure I didn't miss anything, and going through big files line by line to check if that line is throwing something that might be uncaught on runtime is a pain...
Oh yeah, I am using C#, .NET 4.5 and VS2012 PRO.
Thanks good people.
Asked
Active
Viewed 262 times
0

Daniel Gruszczyk
- 5,379
- 8
- 47
- 86
-
1@CodeCaster - it is indeed, thanks man. Somehow I didn't find that one. – Daniel Gruszczyk Nov 18 '13 at 15:38
1 Answers
1
In C#, all objects that are thrown must derive from System.Exception
. If you catch System.Exception
, you catch them all, no matter what subtype.

nvoigt
- 75,013
- 26
- 93
- 142
-
this is not a solution really, I might want to do different things for different exceptions. Plus I don't want to wrap the whole method body/call in one try/catch as I might get 2 the same types of exception at different points, and I might want to do different actions based on that too. But thanks for the answer. – Daniel Gruszczyk Nov 18 '13 at 15:30
-
2You can specialize your try/catch blocks by putting the specific exception's catch blocks first. What you don't want to do is really have one catch block per possible exception. After all, there are tons of potential exceptions (OutOfMemory, DivisionByZero, ArgumentNull...) and you cannot react on them in any sensible way anyway. Catch those exceptions that you can react to and let all others pass. Have a higher layer handle them, until your highest layer just catches all and logs them or something. – nvoigt Nov 18 '13 at 15:33
-
This is fine, and that is what I am doing - the problem is the exceptions that I can't handle or I accidently missed, I would like to add them to the documentation of the method so people using the library and calling that method know what to expect. This is why I simply need a list of uncaught exceptions in a piece of code. This really should be possible via some kind of static analysis or automatic analysis of documentation of methods/objects I have used in my code... I would imagine anyway... – Daniel Gruszczyk Nov 18 '13 at 15:37
-
Java did this using a keyword and there is a good reason the .NET developers did not follow that path: it's not helpful. I guess you can recursively reflect upon the code, but nobody needs it, so I don't think it's an easy, out-of-the-box feature. – nvoigt Nov 18 '13 at 15:44