From 07c333b1f6d6aa3c54aa348576fd654de2af4118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Srbeck=C3=BD?= Date: Tue, 5 Feb 2013 14:05:25 +0000 Subject: [PATCH] Never return null when Debugger.Value is expected - throw GetValueException instead --- src/AddIns/Debugger/Debugger.Core/Eval.cs | 2 +- .../Debugger/Debugger.Core/StackFrame.cs | 5 +++-- src/AddIns/Debugger/Debugger.Core/Value.cs | 22 ++++++------------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/AddIns/Debugger/Debugger.Core/Eval.cs b/src/AddIns/Debugger/Debugger.Core/Eval.cs index 0675c415e7..d605a26d45 100644 --- a/src/AddIns/Debugger/Debugger.Core/Eval.cs +++ b/src/AddIns/Debugger/Debugger.Core/Eval.cs @@ -62,7 +62,7 @@ namespace Debugger case EvalState.Evaluating: throw new GetValueException("Evaluating..."); case EvalState.EvaluatedSuccessfully: return result; case EvalState.EvaluatedException: return result; - case EvalState.EvaluatedNoResult: return null; + case EvalState.EvaluatedNoResult: throw new DebuggerException("Evaluation did not return any value."); case EvalState.EvaluatedTimeOut: throw new GetValueException("Timeout"); default: throw new DebuggerException("Unknown state"); } diff --git a/src/AddIns/Debugger/Debugger.Core/StackFrame.cs b/src/AddIns/Debugger/Debugger.Core/StackFrame.cs index eb27486ea6..35acdd82e7 100644 --- a/src/AddIns/Debugger/Debugger.Core/StackFrame.cs +++ b/src/AddIns/Debugger/Debugger.Core/StackFrame.cs @@ -232,7 +232,7 @@ namespace Debugger if (loc.IsThis) return loc.GetValue(this); } - return null; + throw new GetValueException("The current method does not have 'this'."); } else { return new Value(this.AppDomain, GetThisCorValue()); } @@ -240,7 +240,8 @@ namespace Debugger ICorDebugValue GetThisCorValue() { - if (this.MethodInfo.IsStatic) throw new GetValueException("Static method does not have 'this'."); + if (this.MethodInfo.IsStatic) + throw new GetValueException("Static method does not have 'this'."); ICorDebugValue corValue; try { corValue = CorILFrame.GetArgument(0); diff --git a/src/AddIns/Debugger/Debugger.Core/Value.cs b/src/AddIns/Debugger/Debugger.Core/Value.cs index b1753b7f3e..03ad6157d3 100644 --- a/src/AddIns/Debugger/Debugger.Core/Value.cs +++ b/src/AddIns/Debugger/Debugger.Core/Value.cs @@ -14,6 +14,10 @@ namespace Debugger { public delegate Value ValueGetter(StackFrame context); + /// + /// Thrown when Value can not be obtained. + /// Methods should throw this exception instead of returning null. + /// public class GetValueException: DebuggerException { public GetValueException(string error) : base(error) {} @@ -240,13 +244,13 @@ namespace Debugger } /// Dereferences a pointer type - /// Returns null for a null pointer public Value Dereference() { - if (this.Type.Kind != TypeKind.Pointer) throw new DebuggerException("Not a pointer"); + if (this.Type.Kind != TypeKind.Pointer) + throw new DebuggerException("Not a pointer"); ICorDebugReferenceValue corRef = (ICorDebugReferenceValue)this.CorValue; if (corRef.GetValue() == 0 || corRef.Dereference() == null) { - return null; + throw new GetValueException("Null pointer"); } else { return new Value(this.AppDomain, corRef.Dereference()); } @@ -394,18 +398,6 @@ namespace Debugger } } - /* - /// Get a field or property of an object with a given name. - /// Null if not found - public Value GetMemberValue(Thread evalThread, string name) - { - MemberInfo memberInfo = this.Type.GetMembers(m => m.Name == name && (m.IsFieldOrNonIndexedProperty), GetMemberOptions.None); - if (memberInfo == null) - return null; - return GetMemberValue(evalThread, memberInfo); - } - */ - /// Get the value of given member. public Value GetMemberValue(Thread evalThread, IMember memberInfo, params Value[] arguments) {