This question already has answers here:
How can I determine which exceptions can be thrown by a given method?
(9 answers)
Finding out what exceptions a method might throw in C#
(4 answers)
Closed 9 years ago.
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.
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.
Related
This question already has answers here:
Is it really that bad to catch a general exception?
(15 answers)
Closed 4 years ago.
Why do we use the more specific exceptions like IndexOutOfRangeException
and DivideByZero exceptions when we can catch them in a catch block like this:
try
{
//Some Work
}
catch(Exception E){}
You should only ever write specific code to handle exceptions that you reasonably expect to be thrown. If you understand that specific code could throw a specific type of exception then you can determine exactly what to do in that situation. If you catch absolutely any type of exception then you will have no idea what the cause was and thus you can't know what should be done about it.
This question already has answers here:
What kind of Exceptions cannot be handled? [duplicate]
(2 answers)
Closed 8 years ago.
So from http://msdn.microsoft.com/en-us/library/ms173161.aspx
Once an exception is thrown, it propagates up the call stack until a catch statement for the exception is found.
So the implcation is that all exception typess can be either caught by a catch(ExceptionType) or a generic catch.
However this is plainly not true. For example AccessViolationException bypasses standard exception handling
How to handle AccessViolationException
So what other exceptions also bypass standard exception handling?
I would say that a StackOverflowException is most likely unhandled, I'm not aware of others.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to throw a SqlException(need for mocking)
I want to mock the throwing of SqlException when ExecuteNonQuery is executed;
System.Data.SqlClient.Moles.MSqlCommand.AllInstances.ExecuteNonQuery =
(command) =>
{
throw new MSqlException();
};
This doesn't work as the compiler complains MSqlException doesn't derive from Exception. Am I going about this the wrong way, will I need to wrap ExecuteNonQuery in my code to achieve this?
If you were to throw a SqlException I would expect this to work. It inherits from DbException, which inherits from ExternalException, which inherits from ExternalException.
If you write a custom exception (I am assuming MSqlException is one), you should still inherit from an exception class...
public class MSqlException: SqlException {
The answer was found in another question. The mole needs casting.
stackoverflow.com/a/3795751/771698
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why are Exceptions not Checked in .NET?
Java makes distinction of "checked exception" and "unchecked exception", does C# have the similiar concepts?
C# does not support checked exceptions. You can read up on why the original design did not include checked exceptions.
Link: The Trouble with Checked Exceptions
No, there's no such distinction in C#, nor in many other modern languages, even those that run in the JVM (such as Scala).
From the C# Faq, on MSDN: http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/why-doesn-t-c-have-checked-exceptions.aspx
Nope, it does not. No. Thankfully!
The Trouble With Checked
Exceptions (Anders Hejlsberg,
Bruce Eckel, Bill Venners)
Does Java Need Checked
Exceptions? (Bruce Eckel)
Why doesn't C# have exception
specifications? (Anson Horton)
without the CLR itself supporting checked exceptions, it would be effectively impossible for C# to do so alone.
This question already has answers here:
Is there a way to log or intercept First Chance Exceptions
(3 answers)
Closed 5 years ago.
Is there a way to catch First-Chance exceptions, and log them without running under a debugger?
I suppose another way to ask the question is can I write something that will act like a debugger being attached to my process and see what is going wrong while it happens?
If you are on .NET 4.0, you can use theAppDomain.FirstChanceExceptionevent to get notification of exceptions.