Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 376
m-abdi Avatar asked Feb 20 '13 09:02

m-abdi


People also ask

How do I catch exceptions in Visual Studio?

With a solution open in Visual Studio, use Debug > Windows > Exception Settings to open the Exception Settings window. Provide handlers that respond to the most important exceptions. If you need to know how to add handlers for exceptions, see Fix bugs by writing better C# code.

How do you handle exceptions in C#?

Use a try block around the statements that might throw exceptions. Once an exception occurs in the try block, the flow of control jumps to the first associated exception handler that is present anywhere in the call stack. In C#, the catch keyword is used to define an exception handler.

What kind of blocks are used to handle exceptions?

Associated catch blocks are used to handle any resulting exceptions. A finally block contains code that is run whether or not an exception is thrown in the try block, such as releasing resources that are allocated in the try block. A try block requires one or more associated catch blocks, or a finally block, or both.


2 Answers

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);
like image 113
NDJ Avatar answered Sep 29 '22 00:09

NDJ


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

like image 28
Mamo Ghandi Avatar answered Sep 29 '22 01:09

Mamo Ghandi