How can I handle the exception which throws on an

2020-04-04 03:38发布

问题:

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?

回答1:

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


回答2:

You've probably already tried this, but just in case, have you tried wrapping it in a try catch?

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


回答3:

In case of using external unmanaged\unsafe code, .NET (above .net 4) by default cannot handle Memory Access Violation exceptions that happens inside of dll code. in order to catch these kind of exceptions, there is three things to do. I did them and it worked for me:

  1. Add these Attributes to the method that exception occurred inside of it :
    (the method that calls the method of the unmanaged code.)

    [HandleProcessCorruptedStateExceptions]
    [SecurityCritical]
    
  2. Add this tag to App.Config file below runtime tag :

    <runtime>
    <legacyCorruptedStateExceptionsPolicy enabled="true"/>
    <!-- other tags -->
    </runtime>
    
  3. Catch these kind of exception by using System.AccessViolationException exception type :

      try{
            //Method call that cause Memory Access violation Exeption
         }
    catch (System.AccessViolationException exception)
         {
            //Handle the exception here
          }
    

What i said is just the cure for these type of exception. for more information about this exception's ego and how this approach works, see System.AccessViolationException



回答4:

By putting a try...catch block around every call into the object and its methods.

Something like:

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

Also note that while running under Visual Studio, if you go to the "Debug" menu and select "Exceptions..." it will allow the debugger to break on ALL exceptions if you start your program under the debugger, and not just unhandled exceptions. Just click the 'Thrown' checkbox next to "Common Language Runtime Exceptions".