未处理DivideByZero除了从外部DLL - C#未处理、DivideByZero、DLL

2023-09-04 03:34:50 作者:上帝的宠儿

我有一个C#(.NET 4.0)计划,其主要是要求从外部FTP库方法 - 一个DLL的项目引用。其中的逻辑是在一个try-catch块,并捕获输出错误。异常处理程序有一个通用的参数:赶上(例外前)。该IDE是VS

有时候FTP库零异常抛出以下师。问题是,它是不可以夹在catch块,程序崩溃。 异常起源于我的包装code被发现。任何人有任何想法的区别是什么,以及如何异常可以被捕获?

例外情况:

 说明:该进程被终止,由于未处理的异常。
异常信息:System.DivideByZeroException
堆栈:
   在ComponentPro.IO.FileSystem + c_OU.c_F2B()
   在System.Threading.ExecutionContext.runTry code(System.Object的)
   在System.Runtime.CompilerServices.RuntimeHelpers.Execute codeWithGuaranteedCleanup(试行code,清理code,System.Object的)
   在System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object的,布尔)
   在System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object的)
   在System.Threading.ThreadHelper.ThreadStart()
 

解决方案

中描述了一种类似的问题here也here为解释。至于说在注释的FTP服务器应始终处理违反协议本身没有崩溃之一。如果可以的话,你应该选择另一个FTP。不过,如果你想继续使用该DLL,你需要处理异常的应用程序域级别Blorgbeard指出。

下面的是如何捕获使用的AppDomain例外的例子。 UnhandledException 事件:

 使用系统;
使用System.Security.Permissions;

公共类测试
{

   [的SecurityPermission(SecurityAction.Demand,旗帜= SecurityPermissionFlag.ControlAppDomain)
   公共静态无效的实例()
   {
       AppDomain中currentDomain = AppDomain.CurrentDomain;
       currentDomain.UnhandledException + =新UnhandledExceptionEventHandler(MyHandler的);

       尝试
       {
          抛出新的异常(1);
       }
       赶上(例外五)
      {
         Console.WriteLine(catch子句抓住:+ e.Message);
      }

      抛出新的异常(2);

      //输出:
      // catch子句捕获:1
      //抓MyHandler的:2
   }

  静态无效MyHandler的(对象发件人,UnhandledExceptionEventArgs参数)
  {
     异常E =(异常)args.ExceptionObject;
     Console.WriteLine(抓MyHandler的+ e.Message);
  }

  公共静态无效的主要()
  {
     例();
  }

}
 
远控免杀从入门到实践 4 代码篇 C

I have a C# (.net 4.0) program, whose main is calling methods from an external FTP library - a dll the project references. The logic is in a try-catch block, and the catch prints the error. The exception handler has a generic parameter: catch(Exception ex). The IDE is VS.

Sometimes the FTP library throws the following division by zero exception. The problem is it is not caught in the catch block, and the program crashes. Exceptions originated in my wrapper code are caught. Anyone has any idea what the difference is and how the exception can be caught?

The exception:

Description: The process was terminated due to an unhandled exception.
Exception Info: System.DivideByZeroException
Stack:
   at ComponentPro.IO.FileSystem+c_OU.c_F2B()
   at System.Threading.ExecutionContext.runTryCode(System.Object)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

解决方案

There is a similar problem described here and also here for explanation. As said in one of the comments an FTP server should always handle protocol violations itself without crashing. You should pick another FTP if you can. However, if you want to keep using that DLL you need to handle the exception at App Domain level as Blorgbeard pointed out.

Here an example of how catch the exception using the AppDomain.UnhandledException event:

using System;
using System.Security.Permissions;

public class Test
{

   [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
   public static void Example()
   {
       AppDomain currentDomain = AppDomain.CurrentDomain;
       currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

       try
       {
          throw new Exception("1");
       }
       catch (Exception e)
      {
         Console.WriteLine("Catch clause caught : " + e.Message);
      }

      throw new Exception("2");

      // Output: 
      //   Catch clause caught : 1 
      //   MyHandler caught : 2
   }

  static void MyHandler(object sender, UnhandledExceptionEventArgs args)
  { 
     Exception e = (Exception)args.ExceptionObject;
     Console.WriteLine("MyHandler caught : " + e.Message);
  }

  public static void Main()
  {
     Example();
  }

}