throw 无法抛出异常 是怎么回事

crystal_lz 2015-10-20 10:11:01
这个问题 其实和之前我发过的一篇帖子是同一个问题 一直没有解决掉
http://bbs.csdn.net/topics/391074940

上图是我 F10 单步调试的效果 代码执行了 throw ex 后竟然没有跳出去(我外面有 try catch) 而是直接在下面的花括号上面报了一个错
具体调用环境是这样的
主程序 A.exe
一个公共的Dll文件 common.dll
接口文件 IXXX.dll
插件文件 Plugin.dll(引用 common.dll 访问里面的函数)
A.exe 里面的调用代码代码如下

new Thread(Test) { IsBackground = true }.Start();
//=================
private void Test() {
int i = 0;
IServerVul iv = ServerVulPluginLoader.PluginLoader.GetPluginFromFile(Application.StartupPath + "/testplugin.dll");
//Console.WriteLine(iv.CheckVul("http://fuck.acunetix.com", 0, 5000));
while (true) {//一直循环让他报错(事实上 第一次访问就会出错 以前是看概率)
try {
Console.WriteLine(iv.CheckVul("http://fuck.acunetix.com", 0, 5000));
//用一个不纯在的域名去访问 让它出异常
} catch { }//这里也捕捉不了任何异常
this.BeginInvoke(new MethodInvoker(() => this.Text = (i++).ToString()));
Thread.Sleep(1);
}
}

而访问 网络的代码 差不多就这样

public static xxxx(xx...){
HttpWebRequest...
try{
...
} catch (WebException ex){
//这里就是刚才上面截图部分
}
}

后来我干脆直接 在common.dll 里面 加了一个函数

然后插件里面的代码这样:

public string CheckVul(string strIp, int nPort, int nTimeout) {
try {
WebHelper.Test();//直接调用上面截图中的函数
} catch { }//捕捉不了
return "TRUE";
}

直接 throw 的异常 无法捕捉到
而且 我发现我的插件Plugin.dll在有源码(.pdb)的情况 一切正常 若没有 就是上面这种无法解释的情况
若直接运行 exe 文件 则直接弹出 window关闭对话框
现在不知道要怎么解决了 求办法
最后 下面是上面的插件加载的代码:

public class PluginLoader
{
public static IServerVul GetPluginFromFile(string strFile) {
IServerVul iPlugin = null;
string strSuffix = Regex.Match(strFile, @"\.\w+$", RegexOptions.IgnoreCase).Value.Trim().ToLower();
switch (strSuffix) {
case ".dll": iPlugin = PluginLoader.GetInterfaceFromDotNet(strFile); break;
case ".py": iPlugin = PluginLoader.GetInterfaceFromPython(strFile); break;
default: throw new ArgumentException("Can not load plugin from type[" + strSuffix + "] filename:" + strFile);
}
return iPlugin;
}

public static IServerVul GetInterfaceFromDotNet(string strFileName) {
try {
Assembly asm = Assembly.LoadFile(strFileName);
foreach (var t in asm.GetTypes()) {
if (t.GetInterface("IServerVul") != null) {
return (IServerVul)Activator.CreateInstance(t);
}
}
} catch (ReflectionTypeLoadException ex) {
throw new ReflectionTypeLoadException(ex.Types, ex.LoaderExceptions, ex.Message + "\r\n" + strFileName);
}
return null;
}

public static IServerVul GetInterfaceFromPython(string strFileName) {
return new PyServerVulPlugin(strFileName);
}
}

程序中还有很多类型的插件 都出现这样的情况 简直不知道要如何解决了
...全文
1269 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Poopaye 2015-10-21
  • 打赏
  • 举报
回复
我测试了下,不光是在vs里,直接运行也是捕获不到的
		public static void Test()
		{
			IPlugin i = GetInterfaceFromDotNet(System.IO.Path.Combine(Environment.CurrentDirectory, @"plugin.dll"));
			try
			{
				i.Foo("test");
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex);
			}
		}
crystal_lz 2015-10-21
  • 打赏
  • 举报
回复
引用 11 楼 shingoscar 的回复:
测试了Foo的三种写法
public string Foo(string str)
{
	try	{ StaticMethod.Test(); }
	catch { }
	return "test";
}
public string Foo(string str)
{
	try	{ StaticMethod.Test(); }
	catch { Console.WriteLine("ooo"); }
	return "test";
}
public string Foo(string str)
{
	try	{ StaticMethod.Test(); }
	catch(Exception ex) { Console.WriteLine(ex); }
	return "test";
}
调用的地方
string str = i.Foo("test");
Console.WriteLine(str);
最后都输出了test 在vs里还是会弹出未捕获异常的框 证明运行时这个异常确实被Plugin里的trycatch捕获了,只不过调试时vs认为没有处理,仅此而已,并不影响最终的程序
所以说 我在提出问题的时候 里面 写的是 while(true) 死循环的让他调用(实际上还是没有模拟出我原工程的错误) 因为 在实际我的原工程中 而且像你说的这样 运行的时候感觉上是 确实是被捕获了的 但是 程序运行一段时间 直接弹出windows的关闭对话框 而不是.Net的报错对话框 我原本的程序是在 不停的得到连接 然后去不停的访问连接 整个过程是多线程下 F5 调试运行 就出现那种无法捕捉错误的情况 直接运行可执行程序 运行一段时间 直接崩掉
Poopaye 2015-10-21
  • 打赏
  • 举报
回复
测试了Foo的三种写法
public string Foo(string str)
{
	try	{ StaticMethod.Test(); }
	catch { }
	return "test";
}
public string Foo(string str)
{
	try	{ StaticMethod.Test(); }
	catch { Console.WriteLine("ooo"); }
	return "test";
}
public string Foo(string str)
{
	try	{ StaticMethod.Test(); }
	catch(Exception ex) { Console.WriteLine(ex); }
	return "test";
}
调用的地方
string str = i.Foo("test");
Console.WriteLine(str);
最后都输出了test 在vs里还是会弹出未捕获异常的框 证明运行时这个异常确实被Plugin里的trycatch捕获了,只不过调试时vs认为没有处理,仅此而已,并不影响最终的程序
crystal_lz 2015-10-21
  • 打赏
  • 举报
回复
引用 9 楼 shingoscar 的回复:
我测试了下,不光是在vs里,直接运行也是捕获不到的
		public static void Test()
		{
			IPlugin i = GetInterfaceFromDotNet(System.IO.Path.Combine(Environment.CurrentDirectory, @"plugin.dll"));
			try
			{
				i.Foo("test");
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex);
			}
		}
所以说 这算是bug吗。。。。。心里千万只草泥马奔腾啊。。。。。。这问题不解决 程序无法正常使用啊
本拉灯 2015-10-20
  • 打赏
  • 举报
回复
引用 6 楼 crystal_lz 的回复:
[quote=引用 4 楼 wyd1520 的回复:] 你在调试时当然跳不到外面,这是IDE设置 了,你直接用编译后的exe打开就会跳到外面去了
- -!。。。难道说 所有的 try catch 在调试的时候 都是 不起作用的吗。。。[/quote] 模认你throw ex IDE就会捕获到,所以。。。你看一下IDE调置
crystal_lz 2015-10-20
  • 打赏
  • 举报
回复
引用 3 楼 shingoscar 的回复:
是不是在 调试->异常 里左边那列打了勾?
什么都没有动过
crystal_lz 2015-10-20
  • 打赏
  • 举报
回复
引用 4 楼 wyd1520 的回复:
你在调试时当然跳不到外面,这是IDE设置 了,你直接用编译后的exe打开就会跳到外面去了
- -!。。。难道说 所有的 try catch 在调试的时候 都是 不起作用的吗。。。
crystal_lz 2015-10-20
  • 打赏
  • 举报
回复

更具我的情况 做了一个缩小版的 工程 把这张图标保存 后最改为 zip 里面就是我的工程文件
开发工具 vs 2010
plugin.dll 编译好后 放到 ThrowTest.exe 的目录下
然后F5运行 点击 按钮 正常的
但是 若F5运行后 不去点击 按钮 而是先到 Plugin.dll 的Debug目录下 删除该目录下的所有东西 让它无法找到pdb文件和源文件 再去 点击 按钮 就无法捕捉到异常了 不知道怎么回事
本拉灯 2015-10-20
  • 打赏
  • 举报
回复
你在调试时当然跳不到外面,这是IDE设置 了,你直接用编译后的exe打开就会跳到外面去了
Poopaye 2015-10-20
  • 打赏
  • 举报
回复
是不是在 调试->异常 里左边那列打了勾?
crystal_lz 2015-10-20
  • 打赏
  • 举报
回复
引用 1 楼 starfd 的回复:
要不你换个frame版本试试,只能这么说了
这个也试过了
  • 打赏
  • 举报
回复
要不你换个frame版本试试,只能这么说了

111,111

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧