exception的訊息在那種情況下不會出現 |
答題得分者是:shunchia63
|
rachex2000
一般會員 發表:21 回覆:32 積分:20 註冊:2003-04-28 發送簡訊給我 |
我在程式中一unit寫
[code delphi] unit Unit2; interface uses sysutils; type TMyObject = class(TObject) private FTempPath : string; public constructor Create; destructor Destroy; override; end; var MyObject: TMyObject; implementation constructor TMyObject.Create; begin FTempPath := 'F:\temp\'; if not DirectoryExists(FTempPath) then if not ForceDirectories(FTempPath) then raise Exception.Create('Cannot create ' FTempPath); end; destructor TMyObject.Destroy; begin inherited; end; initialization MyObject := TMyObject .Create; finalization MyObject.Free; end. [/code] 若因為F:\不存在,導致create失敗,發現exception的訊息不會出現,只是程式會提示錯誤然後關閉, 這種寫法那裡有錯?我在help中沒有找到說明,會這樣寫是因為希望在MainForm被create前,先做些準備動作, 不知是否不適合這樣寫 編輯記錄
rachex2000 重新編輯於 2007-11-12 12:37:47, 註解 無‧
rachex2000 重新編輯於 2007-11-13 01:12:14, 註解 無‧ rachex2000 重新編輯於 2007-11-13 01:17:00, 註解 無‧ |
P.D.
版主 發表:603 回覆:4038 積分:3874 註冊:2006-10-31 發送簡訊給我 |
|
rachex2000
一般會員 發表:21 回覆:32 積分:20 註冊:2003-04-28 發送簡訊給我 |
感謝兄臺的測試,我用的是Delphi 6.以上程式碼有些錯誤,已修正,確實在create時會失敗,
不過若用單步執行,還是可看到提示exception的訊息, 直接執行exe,就只有兩個視窗,一個是提示發生錯誤要關閉,然後是runtime error, 不知為何會這樣? ===================引 用 P.D. 文 章=================== 我試過你的程式碼, 並非如你提到沒有出現自己的Create Error, 我測的結果是ok的哦! 查查你的Delphi設定, 不過我不確定那些參數影響到, 因為我用的是Delphi5
編輯記錄
rachex2000 重新編輯於 2007-11-15 10:34:20, 註解 無‧
|
rachex2000
一般會員 發表:21 回覆:32 積分:20 註冊:2003-04-28 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
|
shunchia63
高階會員 發表:26 回覆:141 積分:198 註冊:2007-05-22 發送簡訊給我 |
|
wuabc
初階會員 發表:6 回覆:60 積分:33 註冊:2002-10-28 發送簡訊給我 |
|
rachex2000
一般會員 發表:21 回覆:32 積分:20 註冊:2003-04-28 發送簡訊給我 |
我並不是不懂可以寫在Main Form 的FormCreate事件中,
我只是覺得奇怪,因為help中似乎並無使用raise有限制的說明, 所以有興趣想知道是為何? 另外常聽說GUI與商業logic要分開,如果寫在 FormCreate事件中, 好像又違反此原則,所以才不想寫在這裡, 冒昧說一句,難道Delphi的設計在這裡不夠完善? ===================引 用 wuabc 文 章=================== 寫在Initialization Section的程式碼將在程式啟動階段就被執行, 因此,你的程式碼,在程式完成啟動之前就直接失敗(在Main form 都還來不及Create之前),所以就直接結束掉了(此結論是猜的), 如果將MyObject := TMyObject .Create;改到程式中應該就可以了 (我猜的) |
wuabc
初階會員 發表:6 回覆:60 積分:33 註冊:2002-10-28 發送簡訊給我 |
一個物件在被使用時,才建立(Create),不用時即可拋棄(Free),
這是再正常不過的問題,至少個人如是認為, 不知您以為如何? 請指教 ===================引 用 rachex2000 文 章=================== 我並不是不懂可以寫在Main Form 的FormCreate事件中, 我只是覺得奇怪,因為help中似乎並無使用raise有限制的說明, 所以有興趣想知道是為何? 另外常聽說GUI與商業logic要分開,如果寫在 FormCreate事件中, 好像又違反此原則,所以才不想寫在這裡, 冒昧說一句,難道Delphi的設計在這裡不夠完善? |
shunchia63
高階會員 發表:26 回覆:141 積分:198 註冊:2007-05-22 發送簡訊給我 |
是raise 在 initialization 的問題,跟物件無關 help Raising and handling exceptions Note: Raising an exception in the initialization section of a unit may not produce the intended result. Normal exception support comes from the SysUtils unit, which must be initialized before such support is available. If an exception occurs during initialization, all initialized unitsncluding SysUtilsre finalized and the exception is re-raised. Then the exception is caught and handled, usually by interrupting the program. |
rachex2000
一般會員 發表:21 回覆:32 積分:20 註冊:2003-04-28 發送簡訊給我 |
原來如此,抱歉,我help沒看清楚,感謝兄臺的提示
===================引 用 shunchia63 文 章=================== 是raise 在 initialization 的問題,跟物件無關 help Raising and handling exceptions Note: Raising an exception in the initialization section of a unit may not produce the intended result. Normal exception support comes from the SysUtils unit, which must be initialized before such support is available. If an exception occurs during initialization, all initialized unitsncluding SysUtilsre finalized and the exception is re-raised. Then the exception is caught and handled, usually by interrupting the program. |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |