读书人

有个Exception居然 Catch 不住特高分

发布时间: 2012-01-22 22:38:43 作者: rapoo

有个Exception居然 Catch 不住,特高分请教高手
问题是这样的,我在程序中要调用一个Activex控件,当该控件的运行环境被破坏掉时,在release析构该控件的对象时会抛出一个异常,我就加了try...catch语句,在按F5 调试代码的时候,可以看到这个异常被catch住了,但是如果直接运行可执行文件的时候,这个异常就不能被catch住,好像时被.net framework catch住的,然后提示
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.AxHost.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
Autoloader
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///H:/lib/Debug/Autoloader.exe
----------------------------------------
Connectivity.Common
Assembly Version: 1.0.2564.32478
Win32 Version: 1.0.2564.32478
CodeBase: file:///H:/lib/Debug/Connectivity.Common.DLL
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
.
.
.
.
.
.
.
.
.
.
.
.

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:



<configuration>
<system.windows.forms jitDebugging= "true " />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

很奇怪,不知如何解决,特请教,thanks!

[解决办法]
注册UnhandledExceptionHandler,查查MSDN相关就好了。CodeProject.com上面还有别人给出的通用解决方法可以参考。
[解决办法]
不好说,大致以下几个原因:

1.非托管代码或不安全代码试图对尚未分配的或不具有访问权限的内存进行读操作或写操作,就会发生访问冲突。

2.试图在可验证代码中引用空引用的任何操作都将引发 NullReferenceException,根据平台的不同,也可能表现为AccessViolationException
[解决办法]
AV异常,你的ocx有问题
[解决办法]
activex 自身的错误
导致.net 运行时出错。

[解决办法]
在试图读写受保护内存时引发的异常。
当非托管或不安全代码试图读写未分配或不具有访问权限的内存空间时,就会产生访问冲突。这种情况通常因为指针具有错误的值而发生。并非所有通过错误指针的读写操作都会引发访问冲突,所以访问冲突通常指示已经通过错误指针进行多次读写操作,并且内存内容可能已损坏。因此,访问冲突几乎总是指示存在严重的编程错误。在 .NET Framework 2.0 版中,AccessViolationException 清楚地标识了这些错误。

在完全由可验证托管代码组成的程序中,所有引用都有效或者为空,因而不会产生访问冲突。AccessViolationException 只在可验证托管代码与非托管代码或非安全托管代码交互时才会引发。


试试
try
{
...
}
catch(AccessViolationException ex)
{
....
}

[解决办法]
如果无法预计所有可能出现的Exception类型,那就应该让程序去自动终止它。
catch的原则是不能catch不特定的Exception,如Exception类型,而应该是IOException这样有特定类型的。处于安全考虑,由于不知这种不特定的Exception会导致程序处于何种状态,catch它是有问题的。
[解决办法]
你在什么地方加的Catch?
如果是上面地例子的话,最简单的做法就是冲在WndProc方法,只在其中加个try...catch应该就行了。

[解决办法]
试试这个
catch(COMException ex)

读书人网 >C#

热点推荐