【正文】
ring args[]) { int i = (args[0])。 } } public abstract int read() throws IOException 在程序編譯的時(shí)候必須完成異常的處理 26 ? 何時(shí)會(huì)出現(xiàn)異常 ? ? 由于非預(yù)期的結(jié)果導(dǎo)致系統(tǒng)運(yùn)行時(shí)產(chǎn)生異常 異常 (Exception) class jex7_9 { public static void main(String args[]) { int a = 0。 c = (char)()。 class jex3_3 { public static void main(String args[]) throws IOException { char c。 } catch (fileCloseFailed) { doSomething。 } catch (memoryAllocationFailed) { doSomething。 } catch (fileOpenFailed) { doSomething。 read the file into memory。 determine its size。 close the file。 allocate that much memory。 } 23 ? 為什么采用異常 (Exception) ? 對(duì)錯(cuò)誤進(jìn)行分類,針對(duì)不同錯(cuò)誤作不同處理 異常 (Exception) readFile { open the file。 } } method2 throws exception { call method3。 } method1 { try { call method2。 if (error) return error。 } errorCodeType method3 { errorCodeType error。 if (error) return error。 } errorCodeType method2 { errorCodeType error。 if (error) doErrorProcessing。 } method1 { errorCodeType error。 } method2 { call method3。 } catch (fileCloseFailed) { doSomething。 } catch (memoryAllocationFailed) { doSomething。 } catch (fileOpenFailed) { doSomething。 read the file into memory。 determine its size。t be allocated? ? What happens if the read fails? ? What happens if the file can39。t be opened? ? What happens if the length of the file can39。 close the file。 allocate that much memory。d:\src 19 ? 軟件程序 肯定會(huì) 發(fā)生錯(cuò)誤 /問題 ? what really matters is what happens after the error occurs. How is the error handled? Who handles it? Can the program recover, or should it just die? ? 從外部問題 (硬盤、網(wǎng)絡(luò)故障等 )到編程錯(cuò)誤 (數(shù)組越界、引用空對(duì)象等 ) ? Java語言利用異常來使程序獲得處理錯(cuò)誤的能力 (errorhandling) ? 異常事件 (Exceptional Event) ? An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. 第八講 異常 (Exception) 20 ? 當(dāng)一個(gè) Java程序的方法產(chǎn)生一個(gè)錯(cuò)誤,該方法創(chuàng)造一個(gè)異常對(duì)象并將其交給運(yùn)行系統(tǒng) ? In Java terminology, creating an exception object and handing it to the runtime system is called throwing an exception(拋出異常 ) ? 運(yùn)行系統(tǒng)從錯(cuò)誤發(fā)生處開始尋找處理錯(cuò)誤的程序段 ? The exception handler chosen is said to catch the exception(捕獲異常 ) ? 捕獲異常的過程可以沿方法調(diào)用的逆向順序?qū)ふ? 異常 (Exception) 21 ? 為什么采用異常 (Exception) ? 隔絕正常代碼和錯(cuò)誤處理代碼 異常 (Exception) readFile { open the file。. 執(zhí)行 (在其他目錄 ) d:\ java –classpath d:\src1。 public class Test { . . . }