当抛出NullReferenceException时,检测目标对象是什么 [英] Detecting what the target object is when NullReferenceException is thrown

查看:115
本文介绍了当抛出NullReferenceException时,检测目标对象是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信我们都已经收到了在某个时间或其他时候奇妙模糊的对象引用未设置为对象的实例异常。确定问题的对象通常是设置断点和检查每个语句中的所有成员的繁琐任务。



有没有人可以通过程序手段或其他方式轻松有效地识别导致异常的对象?



- 编辑



似乎我像异常一样模糊。要点是_不必调试应用程序来查找错误的对象。编译器/运行时确实知道对象已被分配/声明,并且对象尚未被实例化。有没有办法在被捕获的例外中提取/识别这些细节?



@ W. Craig Trader



您的说明它是设计问题的结果可能是我能得到的最好的答案。我是强制性的防守编码,并设法摆脱了大部分这些错误后修复了我的习惯随着时间的推移。剩下的那些只是 调整 我没有结束,并导致我向社区发布这个问题。



感谢大家的建议。

解决方案

抛出NRE,没有目标对象 - 这是异常的一点。您最希望的是捕获发生异常的文件和行号。如果您在识别哪个对象引用问题时遇到问题,那么您可能需要重新思考编码标准,因为您似乎在一行代码上做得太多。



更好的解决这种问题的方法是按合同设计,或者通过内建语言结构,或通过图书馆。 DbC将建议预先检查任何传入的参数,以获取超出范围数据的方法(即:Null),并抛出异常,因为该方法将无法使用不良数据。





我认为NRE描述是误导您的。当对象引用为Null时,CLR所遇到的问题是被要求取消引用对象引用。以下示例程序:

  public class NullPointerExample {
public static void Main()
{
对象foo;
System.Console.WriteLine(foo.ToString());
}
}

当你运行它,它会抛出一个NRE在第5行,当它试图评估在foo上的ToString()方法。没有调试对象,只有未初始化的对象引用(foo)。有一个类和一个方法,但没有对象。






Re:Chris Marasti-Georg的answer


$ b $你不应该自己抛出NRE - 这是一个特定含义的系统异常:CLR(或JVM)尝试评估未初始化的对象引用。如果您预先检查对象引用,则会抛出某种无效参数异常或特定于应用程序的异常,而不是NRE,因为您只会混淆下一个维护应用程序的程序员。


I'm sure we all have received the wonderfully vague "Object reference not set to instance of an Object" exception at some time or another. Identifying the object that is the problem is often a tedious task of setting breakpoints and inspecting all members in each statement.

Does anyone have any tricks to easily and efficiently identify the object that causes the exception, either via programmatical means or otherwise?

--edit

It seems I was vague like the exception =). The point is to _not have to debug the app to find the errant object. The compiler/runtime does know that the object has been allocated/declared, and that the object has not yet been instantiated. Is there a way to extract / identify those details in a caught exception

@ W. Craig Trader

Your explanation that it is a result of a design problem is probably the best answer I could get. I am fairly compulsive with defensive coding and have managed to get rid of most of these errors after fixing my habits over time. The remaining ones just tweak me to no end, and lead me to posting this question to the community.

Thanks for everyone's suggestions.

解决方案

At the point where the NRE is thrown, there is no target object -- that's the point of the exception. The most you can hope for is to trap the file and line number where the exception occurred. If you're having problems identifying which object reference is causing the problem, then you might want to rethink your coding standards, because it sounds like you're doing too much on one line of code.

A better solution to this sort of problem is Design by Contract, either through builtin language constructs, or via a library. DbC would suggest pre-checking any incoming arguments for a method for out-of-range data (ie: Null) and throwing exceptions because the method won't work with bad data.

[Edit to match question edit:]

I think the NRE description is misleading you. The problem that the CLR is having is that it was asked to dereference an object reference, when the object reference is Null. Take this example program:

public class NullPointerExample {
  public static void Main()
  {
    Object foo;
    System.Console.WriteLine( foo.ToString() );
  }
}

When you run this, it's going to throw an NRE on line 5, when it tried to evaluate the ToString() method on foo. There are no objects to debug, only an uninitialized object reference (foo). There's a class and a method, but no object.


Re: Chris Marasti-Georg's answer:

You should never throw NRE yourself -- that's a system exception with a specific meaning: the CLR (or JVM) has attempted to evaluate an object reference that wasn't initialized. If you pre-check an object reference, then either throw some sort of invalid argument exception or an application-specific exception, but not NRE, because you'll only confuse the next programmer who has to maintain your app.

这篇关于当抛出NullReferenceException时,检测目标对象是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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