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
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Error:
System.NotImplementedException: The method or operation is not implemented.
Code:
private object Session(string p)
{
throw new NotImplementedException();
}
This is a usual code injected by visual studio, when you require to implement some specified interface. So visual studio generates required method body with this exception inside.
Possible solutions thow:
1) implement it
2) validate: do you really need to derive from the interface your class derives.
3) comment exception raising and return null (unpredicted behavior, based completely on your application structure, good as fast solution, but the worst between all other options)
By simply implementing this method. You are explicitly raising the NotImplementedException exception that's why you are getting error.
Instead of raising exception put your implementation code inside the function or if you don't want to do anything then remove that line and leave the body blank.
private object Session(string p)
{
// Put your code
}
You can either implement the method
OR comment the content and return null like:
private object Session(string p)
{
// throw new NotImplementedException();
return null;
}
This question already has answers here:
Is there a generic constructor with parameter constraint in C#?
(9 answers)
Closed 8 years ago.
I wonder, what rationale is behind lack of generic class type constraints for typed constructors? eg.
public class MyClass<T>
where T : new(int)
{
public T Create(int i)
{
return new T(i);
}
}
Despite fact, that this may be quite easily (though IMO ugly) bypassed (by lambda-ctor), I can imagine no situation, when this constraint might cause any actual trouble or ambiguities.
Notice, that this is a language-structure question, not about a specific problem.
I searched a little bit and found an answer. But since it is here on SO and I don't want to copy it, I will just post a link. It is an answer from Eric Lippert. I hope his answers means something to you.
https://stackoverflow.com/a/9741812/809009
It is somewhat long question there, but you can skip it and read only linked answer.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Create Generic method constraining T to an Enum
Enum type constraints in C#
Consider the following class:
public class Transition<TState>
{
public Transition ()
{
if (!typeof(TState).IsEnum)
throw (new ArgumentException("[TState] has to be of type [System.Enum]."));
}
}
Ideally, this should be declared as:
public class Transition<TState> where TState: System.Enum
{
}
The above, of course, generates a compile-time error. My question is why has that been made illegal. Most sources explain say that it is illegal but do not explain why. Any thoughts?
As Eric Lippert says that and I quote
ALL features are unimplemented until someone designs, specs, implements, tests, documents and ships the feature. So far, no one has done that for this one. There's no particularly unusual reason why not; we have lots of other things to do, limited budgets, and this one has never made it past the "wouldn't this be nice?" discussion in the language design team."
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to use Java-style throws keyword in C#?
i have a function where an exception occurs
say for example
private void functionName() throws Exception
{
// some code that might throw an exception
}
thanks!
No, because there are no checked exceptions in C#
If you are trying to document exceptions that are thrown, use the standard xml documentation
/// <exception cref="InvalidOperationException">Why it's thrown.</exception>
No. There is no such construct in c#. But you can add the comment to your method like this
/// <exception cref="Exception"></exception>
and it will be visible in IntelliSense
Unfortunately there isn't, and it can be a pain. The remedy is to be more careful with the exceptions that your code throws and how you handle errors.