Reputation: 95
Im writing a class and when the argument is misssing I want to throw a exception at the method where the argument is missing.
What Happend: Is the exception gets called in the class, but there is everything fine.
Screen (The exception get called in the class):
Screen (Where I want the exception to be):
Reason: I wanna use this as kind of library, so I want the exception been thrown at the method that is missing something instead of the source code of the class.
Upvotes: 0
Views: 72
Reputation: 3603
You can't, you throw where you throw and that's it. What you could do if you want to separate the exception creation from the handling is create your exception object and NOT throw it but instead RETURN it from your method and have it throw if you wish
Exception MakeException(bool DoThrow, string Message)
{
if(DoThrow)
{
if(string.IsNullOrWhiteSpace(Message)
return new Exception("Message Missing");
else
return new Exception(Message);
}
return null;
}
void Main()
{
var ex = MakeException(true,"MyMessage");
if(ex != null)
throw ex;
}
That way you're creating the exception wherever you want and throwing it at another place.
Upvotes: 0
Reputation: 56536
You can do this by marking your library classes as non-user code and enabling Just My Code. Although note that this configuration means that you can't step into your library's methods.
Upvotes: 3
Reputation: 9825
The exception will be thrown out to where you want it to be. The reason why the debugger stops there is that the debugger has access to the source. If you compile your code as a library and then reference this lib in another app, the debugger will stop at the point where you call the method that threw the exception, because that is the closest point to the exception he can reach.
Upvotes: 1