外部DLL中未处理的DivideByZero异常 - C# [英] Unhandled DivideByZero exception from an external DLL - C#

查看:479
本文介绍了外部DLL中未处理的DivideByZero异常 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#(.net 4.0)程序,其主要是从外部FTP库调用方法 - 项目引用的dll。该逻辑是在一个try-catch块,并且catch打印错误。异常处理程序有一个通用参数: catch(Exception ex)。 IDE是VS。



有时,FTP库会以零异常抛出以下划分。问题是catch块中没有,程序崩溃。
发现我的包装代码的异常被捕获。任何人都知道有什么区别,以及如何捕获异常?



异常:

 说明:由于未处理的异常,进程已终止。 
异常信息:System.DivideByZeroException
堆栈:
在ComponentPro.IO.FileSystem + c_OU.c_F2B()
在System.Threading.ExecutionContext.runTryCode(System.Object)$在System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode,CleanupCode,System.Object)上的
在System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object)中的
,Boolean)
在System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object)
在System.Threading.ThreadHelper.ThreadStart()


解决方案

有一个类似的问题描述此处以及 here for exp lanation。正如在其中一个意见中所说,FTP服务器应该始终处理协议违反本身而不会崩溃。你可以选择另一个FTP。但是,如果您想继续使用该DLL,则需要处理App Domain级别的异常,因为Blorgbeard指出。



这里是使用 AppDomain.UnhandledException 事件:

  using System; 
使用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(异常e)
{
Console.WriteLine(Catch clause catch:+ e.Message);
}

抛出新异常(2);

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

static void MyHandler对象发件人,UnhandledExceptionEventArgs args)
{
异常e =(异常)args.ExceptionObject;
Console.WriteLine(MyHandler catch:+ e.Message);
}

public static void Main()
{
示例();
}

}


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

}

这篇关于外部DLL中未处理的DivideByZero异常 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆