簡體   English   中英

什么時候嘗試捕獲不是嘗試捕獲?

[英]When is a try catch not a try catch?

我有一個有趣的問題,在應用程序關閉期間,try / catch塊似乎在堆棧中被忽略。

我沒有一個正常工作的測試項目(但由於截止日期,否則我會完全嘗試重復此操作),但請考慮以下代碼片段。

class IndexNotFoundException : Exception { }

public static string RunAndPossiblyThrow(int index, bool doThrow)
{
    try
    {
        return Run(index);
    }
    catch(IndexNotFoundException e)
    {
         if(doThrow)
             throw;
    }
    return "";
}

public static string Run(int index)
{
    if(_store.Contains(index))
        return _store[index];
    throw new IndexNotFoundException ();
}

public static string RunAndIgnoreThrow(int index)
{
    try
    {
        return Run(index);
    }
    catch(IndexNotFoundException e)
    {
    }
    return "";
}

在運行期間,這種模式非常有用。 我們得到了依賴於程序控制異常(壞)的代碼的遺留支持,我們可以繼續前進並慢慢刪除用於程序控制的異常。

但是,當關閉我們的UI時,我們會看到從“運行”拋出的異常,即使“doThrow”對於“RunAndPossiblyThrow”的所有當前使用都是假的。 我甚至通過修改代碼看起來像“RunAndIgnoreThrow”來驗證這一點,我仍然會在UI關閉后崩潰。

Eric Lippert先生,我每天都在閱讀你的博客,我很樂意聽到這是一些已知的bug,我不會發瘋。

編輯這是多線程的,我已經驗證所有對象在被訪問時都沒有被修改

編輯明確顯示異常是我們的

編輯忘了提,這是關閉,不幸的是視覺工作室無法直接趕上崩潰。 它可能會崩潰在UI線程以外的線程上,一旦主要關閉,它就會關閉。 我只能通過反復運行和關閉應用程序來調試它,任務管理器打開,“創建轉儲文件”並查看Windbg中產生的400 + mb混亂。 Win7 64供參考。 確保這對你有意義。

編輯

關閉時的以下代碼仍顯示相同的異常。

class IndexNotFoundException : Exception { }

public static string RunAndPossiblyThrow(int index, bool doThrow)
{
    try
    {
        return Run(index);
    }
    catch
    {
    }
    return "";
}

public static string Run(int index)
{
    if(_store.Contains(index))
        return _store[index];
    throw new IndexNotFoundException ();
}

似乎擺脫異常的唯一事情是直接去

class IndexNotFoundException : Exception { }

public static string RunAndPossiblyThrow(int index, bool doThrow)
{
    try
    {
        return Run(index);
    }
    catch
    {
    }
    return "";
}

public static string Run(int index)
{
    if(_store.Contains(index))
        return _store[index];
    return "";
}

當然,例外情況已經消失,但我對瘋狂的恐懼仍然存在。

編輯

它變得更糟......這仍然崩潰......

class IndexNotFoundException : Exception { }

public static string RunAndPossiblyThrow(int index, bool doThrow)
{
    try
    {
        throw new IndexNotFoundException();
    }
    catch
    {
    }
    return "";
}

編輯我有一種明顯的感覺,這將使我無處可去。 除了奇怪的行為之外,我還可以注意到在上述情況下執行UI期間,try catch正在忠實執行。 我的UI沒有崩潰,而且它充滿了空字符串。 但是,一旦我開始關閉UI,崩潰就會顯示出來並且try catch不再阻止異常。

編輯和最終顯然轉儲文件在其中列出了最新的第一次機會異常。 我通過創建一個新項目來驗證這一點,該項目在try catch中睡了10秒鍾。 在等待期間,我得到了.dmp文件,果​​然,我完全被捕獲的異常出現了。

我會為有用的答案標記一些要點,但不幸的是,我的代碼崩潰仍然沒有押韻或原因......

添加一個Exception作為額外的catch。 我認為你得到了一些除ApplicationException之外的其他異常,那就是崩潰你的應用程序。

有各種例外情況無法捕獲。 特別是Stack Overflow! 其中一個可能發生在你的代碼中的某個地方嗎?

http://www.bluebytesoftware.com/blog/PermaLink,guid,223970c3-e1cc-4b09-9d61-99e8c5fae470.aspx

您是否嘗試過添加“finally”子句? 只是為了看看它是否會完全忽略try / catch? 理論上,無論如何,它都應該在它退出try / catch之前跳轉到該行。

如果它仍然忽略它那么它有一些奇怪的東西。

可能你會抓住並再次從Run中拋出一些其他異常。 可能_store是null或者其他東西。

一些可能有助於診斷的事情:

  • 注冊AppDomain.CurrentDomain.UnhandledException,這樣你就可以看到某些東西在UI線程以外的線程上崩潰了

  • 注冊Application.ThreadException以捕獲未在UI線程中捕獲的任何異常

  • 由於這是在關機期間發生的,你是否正在使用任何可能拋出終結器線程的終結器?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM