Pregunta

When you try to throw a new exception from ClassLibrary to another project, Visual studio will open Class1.cs and handle the error.

My question is: How can I throw the exception from the called function(ClassLibrary) to the Caller Function(Windows Form)?

Note : I don't want to throw an original exception , i want to throw a custom exception like this

throw new Exception("change your password")
¿Fue útil?

Solución

I think you have a long long way to go in your basic understanding of Exceptions and Exception handling, I would highly encourage you to pick up a resource on C# development.

To solve your immediate issue, I think the answer you are looking for lies in:

Visual Studio Toolbar->Debug->Exceptions

Visual studio will "break" on various exceptions that you specify, and anything not checked will simply run through the course of whatever try/catch block handles that type of exception.

When you mention:

Note : I don't want to throw an original exception , i want to throw a custom exception like this

throw new Exception("change your password")

That is the definition of a generic exception, not a custom exception. To define a custom exception, you should subclass Exception:

public class MyCustomException : Exception 
{
   ...
}

and then throw it as such:

throw new MyCustomException("Some description of what went wrong");

Then, to get Visual Studio to "break" on that specific exception, find your exception in Toolbar->Debug->Exceptions window (under CLR exceptions) and mark the checkbox next to it.

Otros consejos

throw YourExceptionYouWantToThrow;

or

throw new TypeOfExceptionYouWantToThrow();

The exception will be caught by the next catch block. If there is no try-catch, the application will crash. In case of a visual studio debugger, the debugger will open Class1.cs and display you the source of the exception. You can catch it with something like that:

private void ButtonClick(...)
{
    try{
        Class1.MethodCall();
    }
    catch(Exception ex)
    {
        //Handle the exception here
    }
}

I found the problem.

If you have a source of ClassLibrary project (include with Class1.cs) and you have used the project dll in debug folder, this problem will happen

So if i send to my friend the project assembly (dll only) , the problem will not encounter for him

Is someone have a clearly idea ?

try
{
    YourClass.Function();
}
catch(Exception ex)
{
   // throw your Exception
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top