简体   繁体   English

Try/Catch 块未捕获异常

[英]Try/Catch block not catching Exception

I have a statement inside a try/catch block, but the exception is not getting caught.我在 try/catch 块中有一条语句,但未捕获异常。 Can anyone explain?谁能解释一下?

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

Source Error:源错误:

Line 139:                try
Line 140:                {
Line 141:                    return (int)Session["SelectedLeadID"];
Line 142:                }
Line 143:                catch (Exception ex)

Update This is an ASP.NET application.更新这是一个 ASP.NET 应用程序。 In the catch block, a new exception is thrown.在 catch 块中,抛出了一个新的异常。 The code you see is what is displayed on the ASP.NET error page.您看到的代码就是显示在 ASP.NET 错误页面上的代码。

That catch block should catch the exception, but make sure there's no re-throwing in there.该 catch 块应该捕获异常,但要确保那里没有重新抛出。

Another small comment: I've been tricked quite a few times by VS, cause it breaks on exceptions like that while running in debug-mode.另一个小评论:我已经被 VS 欺骗了很多次,因为它在调试模式下运行时会因为这样的异常而中断。 Try to simply press 'continue' or 'F5' and see if your application doesn't work anyway :)尝试简单地按“继续”或“F5”,看看您的应用程序是否仍然不起作用:)

I suspect you're going to need to add more detail - that isn't reproducible just from your code.我怀疑您将需要添加更多细节 - 这不能仅从您的代码中重现。 In particular (as already noted) we'd need to see inside the catch , and verify that the exception is actually being thrown from inside the try and not somewhere else.特别是(如前所述)我们需要查看catch内部,并验证异常实际上是从try内部而不是其他地方抛出的。

Other possibilities:其他可能性:

  • you have dodgy code inside the exception handler that is itself throwing an exception你在异常处理程序中有狡猾的代码,它本身会抛出异常
  • you have a dodgy Dispose() that is getting called ( using etc)你有一个狡猾的Dispose()被调用( using等)
  • you are in .NET 1.1 and the thing getting thrown (in code not shown) isn't an Exception , but some other object您在 .NET 1.1 中,并且抛出的东西(在未显示的代码中)不是Exception ,而是其他一些对象

If it is only the debugger breaking on the exception and you are using VS2005 or above, you might want to check under Debug->Exceptions... if any of the Common-Language-Runtime-Exceptions are activated.如果只是调试器中断了异常并且您使用的是 VS2005 或更高版本,您可能需要在Debug->Exceptions...下检查是否有任何 Common-Language-Runtime-Exceptions 被激活。 If so, the debugger will always catch the exceptions first, but you are allowed to continue.如果是这样,调试器将始终首先捕获异常,但您可以继续。

To revert to normal execution, simply uncheck the apropriate exceptions from the list.要恢复正常执行,只需从列表中取消选中相应的异常即可。

The code looks terribly ugly IMO.代码看起来非常难看 IMO。 For there to be something in the catch() block means you are going to have another return ... statement which AFAIK you should always have a single return statement at the end of each function to make following code easier.因为在 catch() 块中有一些东西意味着你将有另一个return ...语句,AFAIK 你应该总是在每个函数的末尾有一个 return 语句,以使下面的代码更容易。

ie maybe your code should look like即也许你的代码应该看起来像

public int Function()
{
  int leadID = 0;
  try
  {
    int leadID = (int)Session["SelectedLeadID"];
  }
  catch (Exception ex)
  {
    ...
  }
  return leadID
}

Single exit points are supposed to make the code easier to follow, I guess?单个退出点应该使代码更容易理解,我猜? Anyway, to get any useful help you have to post more of the function.无论如何,要获得任何有用的帮助,您必须发布更多功能。

C# - Visual Studio 2019 C# - Visual Studio 2019

Real case solution:真实案例解决方案:

Use "System.Exception" instead of Exception.使用“System.Exception”代替异常。 --> catch (System.Exception ex) --> catch (System.Exception ex)

I had this exact same problem ( not entering the exception ).我遇到了完全相同的问题(未输入异常)。 The probem was caused by a "using Java.Lang;"该问题是由“使用 Java.Lang;”引起的。 We are using ANDROID SDK inside C#, in this particular project.在这个特定项目中,我们在 C# 中使用 ANDROID SDK。

C# - Visual Studio 2019 C# - Visual Studio 2019

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM