The returned statement after throwing an exception in C #

The following function of the Systems.Collections.Generic.Dictionary class has a return statement after throwing an exception, does anyone have an idea why?

public TValue this[TKey key] { get { int i = FindEntry(key); if (i >= 0) return entries[i].value; ThrowHelper.ThrowKeyNotFoundException(); **return default(TValue);** } set { Insert(key, value, false); } } 
+6
dictionary c #
source share
2 answers

Even

 ThrowHelper.ThrowKeyNotFoundException(); 

Of course it throws an exception, the compiler is not sophisticated enough to prove that it will never do anything. And since he cannot prove that a function never returns, it must assume that it can return.

Eric Lippert just finished a mini-series on his blog entitled "Never Say Never" on this very issue.

http://blogs.msdn.com/b/ericlippert/archive/2011/02/21/never-say-never-part-one.aspx

It turns out that this is a simple case. A problem with a stop , which, as has been shown, is insoluble over Turing machines.

+7
source share

He is forced, because the method itself does not directly rush, ThrowHelper. The compiler does not know this, so return default(TValue); added to satisfy the compiler return default(TValue); although he will never be called.

+3
source share

All Articles