相关疑难解决方法(0)

如何处理AccessViolationException

我在我的.net应用程序中使用COM对象(MODI).我调用的方法抛出一个System.AccessViolationException,它被Visual Studio拦截.奇怪的是我在try catch中包含了我的调用,它包含AccessViolationException,COMException和其他所有东西的处理程序,但是当Visual Studio(2010)拦截AccessViolationException时,调试器会中断方法调用(doc.OCR),如果我单步执行,它将继续到下一行,而不是进入catch块.另外,如果我在visual studio外部运行,我的应用程序崩溃了.如何处理COM对象中引发的此异常?

MODI.Document doc = new MODI.Document();
try
{
    doc.Create(sFileName);
    try
    {
        doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);
        sText = doc.Images[0].Layout.Text;
    }
    catch (System.AccessViolationException ex)
    {
        //MODI seems to get access violations for some reason, but is still able to return the OCR text.
        sText = doc.Images[0].Layout.Text;
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
        //if no text exists, the engine throws an exception.
        sText = "";
    }
    catch
    {
        sText = "";
    }

    if (sText != null)
    {
        sText = sText.Trim();
    } …
Run Code Online (Sandbox Code Playgroud)

.net c# com modi exception

170
推荐指数
3
解决办法
13万
查看次数

SEHException未被Try/Catch捕获

在后台线程中,我的应用程序会定期检查网络文件夹(UNC路径)以获取应用程序更新.它会读出文件的汇编版本,如下所示:

Try
    newVers = System.Reflection.AssemblyName.GetAssemblyName("\\server\app.exe").Version
Catch ex As Exception
    ' ignore
End Try
Run Code Online (Sandbox Code Playgroud)

这段代码经常被执行,到目前为止我总共在多个客户站点猜测超过100.000次没有问题.

有时GetAssemblyName会引发a FileNotFoundException,例如万一网络文件夹无法访问(可能发生并且必须处理).这个异常被Catch下面的块捕获,一切正常.

然而,在三个报告的案例中,GetAssemblyName电话提出了一个SEHException.奇怪的是,这个异常并没有被Catch下面的块捕获,而是被我的全局未处理异常处理程序(System.AppDomain.CurrentDomain.UnhandledException)捕获.结果,应用程序崩溃.

这是异常细节(遗憾的是,我的错误处理例程没有记录异常的ErrorCodeCanResume字段):

Caught exception: System.Runtime.InteropServices.SEHException
   Message: External component has thrown an exception.
   Source: mscorlib
   TargetSite: System.Reflection.AssemblyName nGetFileInformation(System.String)
   StackTrace: 
      at System.Reflection.AssemblyName.nGetFileInformation(String s)
      at System.Reflection.AssemblyName.GetAssemblyName(String assemblyFile)
      at SyncThread.Run() 
      at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
      at System.Threading.ThreadHelper.ThreadStart() …
Run Code Online (Sandbox Code Playgroud)

.net vb.net exception seh

12
推荐指数
1
解决办法
7261
查看次数

通用类型转换器 - TypeConverter或Convert.ChangeType

我试图从String aa泛型转换.泛型类型将是Int32,Int64,Boolean,Double等......我尝试了两种方法:

public static Boolean TryParse<T>(String source, out T value) {

  TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));

  try {

    value = (T)converter.ConvertFromString(source);
    return true;

  } catch {

    value = default(T);
    return false;

  }

}

public static Boolean TryChangeType<T>(Object source, out T value) {

  try {

    Type type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);

    value = (T)Convert.ChangeType(source, type);
    return true;

  } catch {

    value = default(T);
    return false;

  }

}
Run Code Online (Sandbox Code Playgroud)

第二个更通用,因为它接受一个Object.

我也在考虑在TryChangeType中传递一个IFormatProvider,它将在Convert.ChangeType中用于解决文化问题,等等.

你认为第二种方法更好吗?

我可以用任何方式改进我的代码吗?

c#

8
推荐指数
1
解决办法
4011
查看次数

引发异常时Catch Block的行为

是否有任何情况下,当引发异常时跳过catch块

try 
{
  some code
}
catch(Exception ex)
{
  some code
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Exception类,因为它捕获所有异常.

.net c# exception-handling

2
推荐指数
1
解决办法
495
查看次数

标签 统计

.net ×3

c# ×3

exception ×2

com ×1

exception-handling ×1

modi ×1

seh ×1

vb.net ×1