如何处理引发外部DLL的异常? [英] How can I handle the exception which throws on an external dll?
问题描述
private ClsFTPServer _ClsFTPServer;
_ClsFTPServer = new ClsFTPServer(FTPUserName,FTPPassword,FTPPath);
上面的代码创建了一个实例的FTP服务器类,该类在其构造函数上启动FTPserver,它在客户端正确发送请求时作为模块独立工作,但是当FTP服务器发出不正确的请求时,会引发异常并导致我的应用程序崩溃。 / p>
如何处理外部DLL引发的异常以防止我的应用程序崩溃?
我最近回答了一个类似的(ish)问题,可能证明是有用的 -
捕获完全意外的错误
编辑。我必须同意汉斯的评论 - 可能是一个想法,找到另一个FTP服务器。
只是为了完整,这里是appdomain /线程异常设置从 - http://msdn.microsoft.com/en- GB / library / system.windows.forms.application.threadexception.aspx
Application.ThreadException + = new ThreadExceptionEventHandler (ErrorHandlerForm.Form1_UIThreadException);
//设置未处理的异常模式,强制所有Windows窗体错误通过
//我们的处理程序。
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//添加处理非UI线程异常事件的事件处理程序。
AppDomain.CurrentDomain.UnhandledException + =
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
I have developed a project which uses an external dll as FTPServer, I have created the FTP Server on my project like this:
private ClsFTPServer _ClsFTPServer;
_ClsFTPServer = new ClsFTPServer(FTPUserName, FTPPassword, FTPPath);
The Code above creates an instance of FTP server class, the class starts the FTPserver on it's constructor, it works fine independently as a module while the clients send their request correctly, but when an incorrect request comes to FTP server it throws an exception and cause my application to crash.
How can I handle the exception thrown by the external dll to prevent my application from crashing?
I recently answered a similar (ish) question which may prove useful - Catch completely unexpected error
EDIT. I have to agree with Hans' comment above - might be an idea to find another FTP server.
Just for completeness, here's the appdomain/thread exception setup from - 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);
这篇关于如何处理引发外部DLL的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!