How can I handle the exception which throws on an external dll?

后端 未结 4 607
北海茫月
北海茫月 2021-01-18 03:50

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;

相关标签:
4条回答
  • 2021-01-18 04:16

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

    0 讨论(0)
  • 2021-01-18 04:28

    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)
    {
        ...
    }
    
    0 讨论(0)
  • 2021-01-18 04:31

    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);
    
    0 讨论(0)
  • 2021-01-18 04:37

    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

    0 讨论(0)
提交回复
热议问题