如何处理抛出外部DLL的异常?

时间:2013-02-20 09:58:33

标签: c# visual-studio-2010 visual-studio exception exception-handling

我开发了一个使用外部dll作为FTPServer的项目,我在我的项目中创建了这样的FTP服务器:

private ClsFTPServer _ClsFTPServer;
_ClsFTPServer = new ClsFTPServer(FTPUserName, FTPPassword, FTPPath);

上面的代码创建了一个FTP服务器类的实例,该类在它的构造函数上启动FTPserver,它作为一个模块独立工作,而客户端正确发送它们的请求,但是当一个不正确的请求到达FTP服务器时,它抛出一个异常并导致我的应用程序崩溃。

如何处理外部dll抛出的异常以防止我的应用程序崩溃?

4 个答案:

答案 0 :(得分:6)

我最近回答了一个类似的(ish)问题,该问题可能有用 - Catch completely unexpected error

EDIT。我不得不同意Hans上面的评论 - 可能是找到另一台FTP服务器的想法。

为了完整起见,这是来自 - http://msdn.microsoft.com/en-GB/library/system.windows.forms.application.threadexception.aspx的appdomain / thread异常设置

Application.ThreadException += new ThreadExceptionEventHandler  (ErrorHandlerForm.Form1_UIThreadException);

// Set the unhandled exception mode to force all Windows Forms errors to go through 
// our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

// Add the event handler for handling non-UI thread exceptions to the event. 
AppDomain.CurrentDomain.UnhandledException +=
    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

答案 1 :(得分:1)

您可能已经尝试过这个,但为了以防万一,您是否尝试将其包装在try catch中?

try
{
    _ClsFTPServer = new ClsFTPServer(FTPUserName, FTPPassword, FTPPath);
    ...
}
catch(Exception e)
{
    ...
}

答案 2 :(得分:1)

如果使用外部非托管\不安全代码,则.NET(高于.net 4)默认情况下无法处理dll代码内部发生的内存访问冲突异常。 为了捕获此类异常,需要做三件事。我做了它们,对我有用:

  1. 将这些属性添加到其中发生异常的方法中:
    (调用非托管代码方法的方法。)

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        unsigned int k;
        int x,a,target;
        vector<int>list;
        cout << "Enter the amount of numbers you want to enlist \n";
        cin >> k;
        while((list.size()< k) && (cin >> x)){
            list.push_back(x);
        }
        sort(list.begin(), list.end());
        cout << "Enter the target that you want to search for \n";
        cin >> target;
        auto result = lower_bound(list.begin(), list.end(), target);
        if(result == list.end()){
            cout << "Your result is not found ";
        }
        else{
            cout << "Your result is found at the index: " << result-list.begin();
        }
        return 0;
    }
    
  2. 将此标签添加到运行时标签下面的 App.Config 文件中:

    [HandleProcessCorruptedStateExceptions]
    [SecurityCritical]
    
  3. 使用System.AccessViolationException异常类型捕获此类异常:

    <runtime>
    <legacyCorruptedStateExceptionsPolicy enabled="true"/>
    <!-- other tags -->
    </runtime>
    

我所说的只是解决这类异常的方法。有关此异常的自我以及该方法的工作原理的更多信息,请参见System.AccessViolationException

答案 3 :(得分:-3)

通过在对对象及其方法的每次调用周围放置一个try ... catch块。

类似的东西:

try
{
    // use the DLL in some way
}
catch (Exception e) 
{
    // Handle the exception, maybe display a warning, log an event, etc.)
}

另请注意,在Visual Studio下运行时,如果您转到&#34; Debug&#34;菜单并选择&#34;例外...&#34;如果你在调试器下启动你的程序,它将允许调试器在所有异常中断,而不仅仅是未处理的异常。只需点击“投掷”即可。 &#34; Common Language Runtime Exceptions&#34;。

旁边的复选框
相关问题