Try and Catch class Try Catch public static
Try and Catch class Try. Catch{ public static void main(String[] args){ try{ String[] book. Name = {"JAVA程式設計", "JSP程式設計"}; System. out. println(book. Name[2]); System. out. println("是本好書!"); } catch(Array. Index. Out. Of. Bounds. Exception e){ System. out. println("例外事件: 陣列索引值超過界限!"); } catch(Exception e){ System. out. println("例外事件!"); } System. out. println("例外事件已解決,繼續執行. . . "); } } 例外事件: 陣列索引值超過界限! 例外事件已解決,繼續執行. . .
import java. io. *; public class Use. Exception { public static void main(String[] args) { try { int input; Buffered. Reader buf = new Buffered. Reader( new Input. Stream. Reader(System. in)); System. out. print("請輸入整數: "); input = Integer. parse. Int(buf. read. Line()); System. out. println("input x 10 = " + (input*10)); } catch(IOException e) { System. out. println("I/O錯誤"); } catch(Number. Format. Exception e) { System. out. println("輸入格式有誤"); } } }
import java. io. *; public class Use. Exception { public static void main(String[] args) { try { int input; Buffered. Reader buf = new Buffered. Reader( new Input. Stream. Reader(System. in)); System. out. print("請輸入整數: "); input = Integer. parse. Int(buf. read. Line()); System. out. println("input x 10 = " + (input*10)); } catch(IOException e) { System. out. println("I/O錯誤"); } catch(Number. Format. Exception e) { System. out. println("輸入格式有誤"); } } }
class Try. Catch. Finally{ public static void main(String[] args){ try{ String[] book. Name = {"JAVA程式設計", "JSP程式設計"}; System. out. println(book. Name[2]); System. out. println("是本好書!"); } catch(Array. Index. Out. Of. Bounds. Exception e){ System. out. println("例外事件: 陣列索引值超過界限!"); } catch(Exception e){ System. out. println("例外事件!"); } finally{ System. out. println("finally區塊內容一定會執行!"); } System. out. println("例外事件已解決,繼續執行. . . "); } }
class Book. Info{ void show. Name(String[] name) throws Array. Index. Out. Of. Bounds. Exception{ System. out. println(name[2]); } void show. Price(double[] price) throws Array. Index. Out. Of. Bounds. Exception{ System. out. println(price[2]); } } class Throws. Ex{ public static void main(String[] args){ Book. Info book 1 = new Book. Info(); String[] book. Name = {"JAVA程式設計", "JSP程式設計"}; double[] book. Price = {550, 600}; try{ book 1. show. Name(book. Name); book 1. show. Price(book. Price); } catch(Array. Index. Out. Of. Bounds. Exception e){Exception. Handler. array. Index(); } catch(Exception e){Exception. Handler. exception(); } } } class Exception. Handler{ public static void array. Index(){ System. out. println("例外事件: 陣列索引值超過界限!"); } public static void exception(){ System. out. println("例外事件!"); } }
class Book. Exception extends Runtime. Exception{ public Book. Exception(String msg){super(msg); } } class Book{ private double price = 100. 0; public void set. Price(double price) throws Book. Exception{ if(price<100. 0 || price>1000. 0) throw new Book. Exception("定價設定錯誤, 不可為" + price); else this. price = price; } public double get. Price(){return price; } } class Custom. Ex{ public static void main(String[] args){ Book book 1 = new Book(); try{book 1. set. Price(99. 0); } catch(Book. Exception e){System. out. println(e); } } }
- Slides: 21