全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:8282
推到 Plurk!
推到 Facebook!

請問try和catch要怎麼使用?!

答題得分者是:jessechan
kenano
一般會員


發表:6
回覆:5
積分:2
註冊:2002-11-25

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-12-05 11:11:19 IP:140.118.xxx.xxx 未訂閱
請問大家,我不太懂try,catch的用法 catch()括號中參數要怎麼設定? 假設 try { outport="c:\\" name ".txt"; s1=fopen(outport.c_str(),"r"); } 如果沒有name這個檔案,我希望它去讀取name1.txt 但是如果存在bug1.txt檔案的話,它會去讀取name2.txt 這樣的話是不是要設定2個catch, catch括號中的東西要怎麼寫?! 謝謝~
RaynorPao
版主


發表:139
回覆:3622
積分:7025
註冊:2002-08-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-12-05 11:20:19 IP:203.73.xxx.xxx 未訂閱
引言: 請問大家,我不太懂try,catch的用法 catch()括號中參數要怎麼設定? 假設 try { outport="c:\\" name ".txt"; s1=fopen(outport.c_str(),"r"); } 如果沒有name這個檔案,我希望它去讀取name1.txt 但是如果存在bug1.txt檔案的話,它會去讀取name2.txt 這樣的話是不是要設定2個catch, catch括號中的東西要怎麼寫?! 謝謝~
kenano 你好: 以下內容轉貼自 bcb help < class="code"> The try block contains a statement or statements that can throw an exception. A program throws an exception by executing a throw statement. The throw statement generally occurs within a function. For example: void SetFieldValue(DF *dataField, int userValue) { if ((userValue < 0) || userValue > 10) throw EIntegerRange(0, 10, userValue); . . . } Another part of the program can catch the thrown exception object and handle it accordingly. For example: try { SetFieldValue(dataField, userValue); } catch (EIntegerRange &rangeErr) { printf("Expected value between %d and %d, but got %d\n", rangeErr.min, rangeErr.max, rangeErr.value); } In the previous example, if the function SetFieldValue finds that its input parameters are invalid, it can throw an exception to indicate this. The try/catch block wrapping SetFieldValue to catch the exception that SetFieldValue throws, and executes the printf statement. If no exception is thrown, the printf statement will not be executed. A try block specified by try must be followed immediately by the handler specified by catch. The try block is a statement that specifies the flow of control as the program executes. If an exception is thrown in the try block, program control is transferred to the appropriate exception handler. The handler is a block of code designed to handle the exception. The C language requires at least one handler immediately after a try block. The program should include a handler for each exception that the program can generate. 備註: 如果一開始不知道 catch() 裡面要寫什麼 就寫 catch(...) 抓全部的 exception --
------
-- 若您已經得到滿意的答覆,請適時結案!! --
-- 欲知前世因,今生受者是;欲知來世果,今生做者是 --
-- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 --
jessechan
版主


發表:109
回覆:394
積分:254
註冊:2002-04-05

發送簡訊給我
#3 引用回覆 回覆 發表時間:2002-12-05 11:55:42 IP:203.75.xxx.xxx 未訂閱
其實您的問題很簡單, 只要用 FileExists 去判斷, 如果您真的要 catch 的話, fopen 也不會讓您 catch 到任何東西的, 此外, 在 BCB 中, 我認為儘可能用 File... 開頭的函數來處理檔案, 不要再用 fopen 了. Jesse Chan
------
Jesse Chan
jessechan
版主


發表:109
回覆:394
積分:254
註冊:2002-04-05

發送簡訊給我
#4 引用回覆 回覆 發表時間:2002-12-05 13:15:17 IP:203.75.xxx.xxx 未訂閱
try, catch 的用法簡單說明如下, 第一種 try { 需要有例外處理的程式碼... } catch (...) { 發生例外時要如何處理的程式碼... } 在 try 的區塊中, 只要有任何例外 (exception) 產生, 如零除, access violation... 等, 就會直接跑到 catch 中去執行, 如果這個例外不是自行寫程式產生 if (某種狀況發生了) throw Exception("例外的說明"); 則由 BCB 的 run time 機制會在一些例外發生時自動 throw 出來, 系統內建有很多繼承自 Exception 的 class, 表列如下 EAbort Stops a sequence of events without displaying an error message dialog box. EAccessViolation Checks for invalid memory access errors. EBitsError Prevents invalid attempts to access a Boolean array. EComponentError Signals an invalid attempt to register or rename a component. EConvertError Indicates string or object conversion errors. EDatabaseError Specifies a database access error. EDBEditError Catches data incompatible with a specified mask. EDivByZero Catches integer divide-by-zero errors. EExternalException Signifies an unrecognized exception code. EInOutError Represents a file I/O error. EIntOverflow Specifies integer calculations whose results are too large for the allocated register. EInvalidCast Checks for illegal typecasting. EInvalidGraphic Indicates an attempt to work with an unrecognized graphic file format. EInvalidOperation Occurs when invalid operations are attempted on a component. EInvalidPointer Results from invalid pointer operations. EMenuError Involves a problem with menu item. EOleCtrlError Detects problems with linking to ActiveX controls. EOleError Specifies OLE automation errors. EPrinterError Signals a printing error. EPropertyError Occurs on unsuccessful attempts to set the value of a property. ERangeError Indicates an integer value that is too large for the declared type to which it is assigned. ERegistryException Specifies registry errors. EZeroDivide Catches floating-point divide-by-zero errors. 這些 class 提供了一組好用的方法, 如 ClassName(), Message() 等, 可供您了解系統出了什麼狀況. 您也可以直接 catch 您想要的 exception 如 catch (const EIntOverflow &E) { 發生例外時要如何處理的程式碼... } 只針對整數 overflow 時做處理. 此外還有一種有用的 keyword, __finally { 無論發生什麼例外都要執行的程式碼... } 只不過 catch 及 __finally 不能在同一個程式區塊中使用. 不知道這樣說明有沒有增加您的了解. Jesse Chan
------
Jesse Chan
kenano
一般會員


發表:6
回覆:5
積分:2
註冊:2002-11-25

發送簡訊給我
#5 引用回覆 回覆 發表時間:2002-12-05 19:29:05 IP:140.118.xxx.xxx 未訂閱
感謝前輩的解答~~ 有時候真的覺得拿書閉門造車實在是太慢了.. 上網請教有經驗的人,效率果然差很多!!!
lcsboy
版主


發表:87
回覆:622
積分:394
註冊:2002-06-18

發送簡訊給我
#6 引用回覆 回覆 發表時間:2002-12-05 19:53:49 IP:210.58.xxx.xxx 未訂閱
引言: 感謝前輩的解答~~ 有時候真的覺得拿書閉門造車實在是太慢了.. 上網請教有經驗的人,效率果然差很多!!!
系統時間:2024-04-23 17:33:28
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!