简体   繁体   English

从ClassLibrary抛出异常

[英]Throw exception from ClassLibrary

When you try to throw a new exception from ClassLibrary to another project, Visual studio will open Class1.cs and handle the error. 当您尝试将新的异常从ClassLibrary抛出到另一个项目时,Visual Studio将打开Class1.cs并处理错误。

My question is: How can I throw the exception from the called function(ClassLibrary) to the Caller Function(Windows Form)? 我的问题是:如何将异常从被调用函数(ClassLibrary)扔给调用者函数(Windows窗体)?

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")

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. 我认为您对Exception和Exception处理的基本理解还有很长的路要走,我强烈建议您选择有关C#开发的资源。

To solve your immediate issue, I think the answer you are looking for lies in: 为了解决您眼前的问题,我认为您正在寻找的答案在于:

Visual Studio Toolbar->Debug->Exceptions Visual Studio工具栏->调试->异常

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. Visual Studio会“破坏”您指定的各种异常,并且任何未检查的内容都将简单地通过任何try / catch块处理该异常类型的过程进行。

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: 要定义自定义异常,应将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. 然后,要使Visual Studio针对该特定异常“中断”,请在“工具栏”->“调试”->“异常”窗口(在CLR异常下)中找到您的异常,并标记它旁边的复选框。

throw YourExceptionYouWantToThrow;

or 要么

throw new TypeOfExceptionYouWantToThrow();

The exception will be caught by the next catch block. 异常将被下一个catch块捕获。 If there is no try-catch, the application will crash. 如果没有try-catch,应用程序将崩溃。 In case of a visual studio debugger, the debugger will open Class1.cs and display you the source of the exception. 如果是Visual Studio调试器,则调试器将打开Class1.cs并向您显示异常源。 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 如果您有ClassLibrary项目的源代码(包括在Class1.cs中),并且已在调试文件夹中使用了项目dll,则将发生此问题。

So if i send to my friend the project assembly (dll only) , the problem will not encounter for him 因此,如果我将项目程序集(仅用于dll)发送给我的朋友,那么他将不会遇到问题

Is someone have a clearly idea ? 有人有明确的主意吗?

try
{
    YourClass.Function();
}
catch(Exception ex)
{
   // throw your Exception
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM