C#尝试捕获在某些机器上不起作用 [英] C# try catch not working on some machines

查看:132
本文介绍了C#尝试捕获在某些机器上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了解一些从Web请求获得响应的代码.以下是代码

I have a try catch around some code that gets a response from a web request. The following is the code

var token = "";
try
  {
    var response = req.GetResponse();
    using (var sReader = new streamReader(response.GetResponseStream()))
    {  
       token = sReader.ReadToEnd();
     }
catch (webException we)
{
   token = "ERROR: " + we;
}
catch (Exception e)
{
   Token = "ERROR: " + e;
}




这行-




This line -

var response = req.GetResponse();

是失败的那一行.它失败的事实是我正在与另一个小组合作的另一个问题.

问题是catch语句仅捕获3台测试计算机中的2台错误.第三个没有捕获到,并且出现未处理的异常错误.

我尝试过的事情:

我在某个地方读到该问题可能是.NET版本不匹配,因此我确保该应用程序的目标是.NET 4.6,并且所有3台计算机均已安装4.6或更高版本.

机器1-Windows 10,.NET 4.6.2-捕获块工作
机器2-Server 2008,.NET 4.6-捕获块工作
机器3-Server 2008,.NET 4.6-catch块不起作用.

我还能看什么?

is the one that fails. The fact it fails is a different issue that I''m working on with a different group.

The problem is the catch statement only catches the error on 2 of the 3 test machines. The third one it doesn''t catch and I get a unhandled exception error.

What I have tried:

Somewhere I read that the issue could be a mismatch .NET version, so I''ve made sure the app targets .NET 4.6 and all 3 machines have 4.6 or greater installed.

Machine 1 - Windows 10, .NET 4.6.2 - catch block works
Machine 2 - Server 2008, .NET 4.6 - catch block works
Machine 3 - Server 2008, .NET 4.6 - catch block does not work.

What else can I look at?

推荐答案

听起来好像抛出的异常不在您正在查看的代码范围之内.您需要以其他方式捕获错误.

我有我使用的应用程序范围的异常处理程序.您可以在本文中找到WinForm和WPF实现的示例:适用于Winform&的静音ClickOnce安装程序C#和WPF中的WPF VB [ ^ ]
It sounds like the exception thrown is outside the area of code that you are looking at. You need to catch the error a different way.

I have app wide exception handlers that I use. You can find examples of WinForm and WPF implementation in this article: Silent ClickOnce Installer for Winform & WPF in C# & VB[^]


这篇关于C#尝试捕获在某些机器上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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