2010年1月27日 星期三

BUG-VS2008在x64 OS下會忽略Form_OnLoad的未處理例外

BUG-VS2008在x64 OS下會忽略Form_OnLoad的未處理例外: "
昨天河道上看到噗友bauann為了VS2008在W7 x64下無法順利運作,重灌到滿頭大汗外加內心淌血。我原本以為這是一起"抽中籤王"的個案,因為我公司與家裡的環境分別是Win2008 x64 + VS2008與W7 x64 + VS2008,印象中都可以正常使用。(若公司機器的VS2008有問題還混到現在,應該早就被炒魷魚了 XD)
不過,噗裡引用了一篇論壇討論,卻讓我想起一些東西...
One VERY important point is that this issue is only on exceptions raised during the Forms.OnLoad event handler (or methods called from within this event handling).
回憶起確實遇過Form_Load階段有錯誤,卻沒被VS2008抓到的經驗,當時很狐疑是否VS2008某個設定沒調好才會這個樣子,但因為不致"卡關",便未深究。
bauann提供的資訊解答了我的疑惑,我寫了一個WinForm Project來驗證:
using System;


using System.Windows.Forms;


using System.IO;




namespace WindowsFormsApplication1


{


public partial class Form1 : Form


{


public static string InvalidPath =


"1:\\invlid_path.txt";




public Form1()


{


InitializeComponent();


}




private void Form1_Load(object sender, EventArgs e)


{


MessageBox.Show("Form1_Load 1");


string s = File.ReadAllText(InvalidPath);


MessageBox.Show("Form1_Load 2");


}




private void button1_Click(object sender, EventArgs e)


{


string s = File.ReadAllText(InvalidPath);


}


}


}




果不其然,MessageBox “Form1_Load 1”後,接著就進入WinForm操作界面。理論上在File.ReadAllText那一列就該因檔案路徑亂給而發生Exception,但在x64 OS下的VS2008卻若無其事地忽略之後的程式碼MessageBox.Show(“Form_Load 2”)繼續執行。在button1_Click裡,相同的程式碼,Unhandled Exception倒可以正確地被VS2008捕捉。


由MSDN論壇的討論得知,這確認是一個VS2008的Bug,目前尚無HotFix。但有幾種解決方法:


1) 設定Exception throw時就攔截(如下圖),但這會導致每一個try { … }遇到的Exception也觸發中斷。





2) 在Form_Load()第一列加入System.Diagnostics.Debugger.Break(),然後按Ctrl-F5跑程式(Start without Debugging),如此Debugger會事後掛上去,就可攔到Unhandled Exception。


以上兩個方法都不是頂好的根治之道,只能稍稍止痛,依目前的產品發展時程,我認為這個Bug被修復的機會不太大。但由於問題出現時機只限於Form_OnLoad事件,在使用Windows x64 + VS2008寫Windows Form Project時多加留意,I think I can live with it。

"

沒有留言: