2011-11-23 71 views
2

當我調試我的申請,我得到了很多InvalidOperationException異常和NullReferenceException異常的像這樣:出現InvalidOperationException和NullReferenceException異常

A first chance exception of type 'System.NullReferenceException' occurred in XGen.Framework.DLL 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.NullReferenceException' occurred in XGen.Framework.DLL 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.NullReferenceException' occurred in XGen.Framework.DLL 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.NullReferenceException' occurred in XGen.Framework.DLL 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.NullReferenceException' occurred in XGen.Framework.DLL 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.NullReferenceException' occurred in XGen.Framework.DLL 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll 
A first chance exception of type 'System.NullReferenceException' occurred in XGen.Framework.DLL 

它使應用程序的一些慢怎麼辦?

編輯:

發現那裏的出現InvalidOperationException破天

public static Value.Locale Get(string value) 
    { 
     try 
     { return _Items.First(itm => itm.ID.ToUpper() == value.ToUpper() || itm.Name.ToUpper() == value.ToUpper()); } 
     catch (Exception) 
     { return new XGen.Framework.Value.Locale(); } 
    } 

翻譯文本:該序列不包含任何元素匹配

我應該檢查是否_Items.Count> 0?

+0

您是否已經能夠追查這些異常被拋出的位置? –

+3

幾乎可以肯定。你會想跟蹤這些。 – drdwilcox

+0

nope ...我不知道他們在哪裏發生,但我對應用程序沒有任何問題...我想我會有很多工作在尋找他們... :( – ShadowG

回答

2

好吧,如果他們不使你的程序慢,他們應該是例外。 如果你知道你的_items可以是空的,這不是一個例外,這是一個正常的流程,它不應該被異常處理。 您可以檢查計數,或者只需撥打FirstOrDefault而不是First ...

+0

我正要回答我自己的問題。 問題出現在我面前,我沒有看到。我所要做的就是使用FirstOrDefault而不是First。 – ShadowG

+0

它發生,有時我們需要一個外部的眼睛:) – MBen

3

它不可能使你的應用程序更快,所以是的,它會讓它變慢,但當然「更慢」是相對的。我會更關心這些異常是由於應用程序中的邏輯錯誤而造成的,而不是它運行的速度。

+0

因爲我有很多這樣的例外......我認爲它必須在基類或基類組件類的某個地方。當我說很多時,我的意思真的很多 – ShadowG

1

您必須開始縮小這些範圍,以弄清楚發生了什麼。我的猜測是導致這個XGen.Framework.DLL做的不好(在System.Core中導致InvalidOperationException),但是爲你正常處理它,這就是應用程序繼續運行的原因。

您可以讓調試器在第一次更改異常時停止,並檢查堆棧跟蹤。

+0

如何告訴調試器這麼做 – ShadowG

+0

你必須在調試器選項中取消選中「只是我的代碼」。閱讀本文(Visual Studio 2005相關,但在2010年AFAIK中是一樣的)http://msdn.microsoft.com/en-us/library/d14azbfh(v=vs.80).aspx – zmbq

+0

thx它幫助我找到哪裏他們正在發生..編輯代碼顯示哪裏 – ShadowG

0

first chance exception意味着代碼中的某些異常被拋出,但被捕獲和處理。在你的情況下,你會得到很多NullReferenceExceptionInvalidOperationException,它大部分時間表示某處有bug(屬性或字段未初始化,調用處於無效狀態的對象)。所以我不會爲速度而擔心,但更多的是關於你的情況的正確性。

1

請參閱從喬恩斯基特這個答案,他覺得合理使用的時候不減速的應用程序,但你情況似乎並不正常:

如果你有機會到這裏的例外是點顯着的 會損害您的表現,您在使用 例外情況方面存在問題,而不僅僅是表現。

How slow are .NET exceptions?

相關問題