跳至內容

Java 程式設計/關鍵字/catch

來自維基文庫,一個自由的圖書館

catch 是一個關鍵字。

它是 try 塊的一部分。如果在 try 塊中丟擲一個異常,則該異常會被與塊的任何 catch 部分進行比較。如果異常與 catch 部分中的一個異常匹配,則異常將在此處得到處理。

例如

Computer code
try {
   //...
     throw new MyException_1();
   //...
} catch ( MyException_1 e ) {
   // --- Handle the Exception_1 here --
} catch ( MyException_2 e ) {
   // --- Handle the Exception_2 here --
}

參見

華夏公益教科書