如何处理抛出外部dll的exception?

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

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

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

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

我最近回答了一个类似的(ish)问题,该问题可能有用 – 抓住完全出乎意料的错误

编辑。 我不得不同意Hans上面的评论 – 可能是找到另一个FTP服务器的想法。

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

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

你可能已经尝试了这个,但为了以防万一,你试过把它包装在try catch吗?

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

通过在每个调用对象及其方法的周围放置一个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下运行时,如果转到“Debug”菜单并选择“Exceptions …”,如果在调试器下启动程序,调试器将允许调试器中断所有exception,而不仅仅是未处理的exception。 只需单击“Common Language Runtime Exceptions”旁边的“Thrown”复选框即可。