- public class Exception30 {
- public static void main(String[] args) {
- System.out.println("*** start ***");
- int a = Integer.parseInt(args[0]);
- System.out.println(100 / a);
- System.out.println("*** end ***");
- }
- }
□ 実行結果
$ java Exception30 *** start *** Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Exception30.main(Exception30.java:4) $ java Exception30 Hello *** start *** Exception in thread "main" java.lang.NumberFormatException: For input string: "Hello" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Exception30.main(Exception30.java:4) $ java Exception30 0 *** start *** Exception in thread "main" java.lang.ArithmeticException: / by zero at Exception30.main(Exception30.java:5) $ java Exception30 20 *** start *** 5 *** end ***
- public class Exception50 {
- public static void main(String[] args) {
- System.out.println("*** start ***");
- int a = 0;
- try{
- a = Integer.parseInt(args[0]);
- System.out.println(100 / a);
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.print("*** 不正なインデックス *** ");
- System.out.println(e.getMessage());
- } catch (Exception e) {
- System.out.println("*** Exception ***");
- e.printStackTrace();
- }
- System.out.println("*** end ***");
- }
- }
□ 実行結果
$ java Exception50 *** start *** *** 不正なインデックス *** 0 *** end *** $ java Exception50 Hello *** start *** *** Exception *** java.lang.NumberFormatException: For input string: "Hello" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Exception50.main(Exception50.java:6) *** end *** $ java Exception50 0 *** start *** *** Exception *** java.lang.ArithmeticException: / by zero at Exception50.main(Exception50.java:7) *** end *** $ java Exception50 20 *** start *** 5 *** end ***
- // 1211 = 1201
- // package product;
- // 商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class Product extends AbstractProduct {
- private int price; // 商品定価
- // 商品クラスのコンストラクター。
- public Product(String code, String name, int price) {
- setCode(code);
- setName(name);
- setPrice(price);
- }
- // 商品クラスのコンストラクター。
- public Product(String code, String name, String price) {
- setCode(code);
- setName(name);
- setPrice(price);
- }
- // 商品定価に値を設定します。
- public void setPrice(int price) {
- if (price < 0) {
- this.price = 0;
- return;
- }
- this.price = price;
- }
- // 商品定価に値を設定します。
- public void setPrice(String price) {
- try {
- setPrice(Integer.parseInt(price));
- } catch (NumberFormatException e) {
- setPrice(0);
- e.printStackTrace();
- } catch (Exception e) {
- setPrice(0);
- e.printStackTrace();
- }
- }
- // 商品定価の値を取得します。
- public int getPrice() {
- return this.price;
- }
- }
- // 1211
- // package application;
- // import product.*;
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- Product p1 = new Product(args[0], args[1], args[2]);
- System.out.println("商品のインスタンスを生成しました。 p1 = " + p1);
- System.out.println(" code = " + p1.getCode());
- System.out.println(" name = " + p1.getName());
- System.out.println(" price = " + p1.getPrice());
- }
- }
□ 実行結果
$ java TestProduct A001 パソコン 188000 商品のインスタンスを生成しました。 p1 = product.Product@1b90b39 code = A001 name = パソコン price = 188000 $ java TestProduct A001 パソコン 188ooo java.lang.NumberFormatException: For input string: "188ooo" at java.lang.NumberFormatException.forInputString(NumberFormatException. java:48) at java.lang.Integer.parseInt(Integer.java:435) at java.lang.Integer.parseInt(Integer.java:476) at product.AbstractProduct.setPrice(AbstractProduct.java:42) at product.Product.(Product.java:23) at application.TestProduct.main(TestProduct.java:7) 商品のインスタンスを生成しました。 p1 = product.Product@18fe7c3 code = A001 name = パソコン price = 0
- // 1211
- // package product;
- // 割引商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class DiscountProduct extends Product implements DiscountInterface {
- private int discount; // 商品割引率
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, int price) {
- this(code, name, price, 0);
- }
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, String price) {
- this(code, name, price, "0");
- }
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, int price, int discount) {
- super(code, name, price);
- setDiscount(discount);
- }
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, String price, String discount) {
- super(code, name, price);
- setDiscount(discount);
- }
- // 商品割引率に値を設定します。
- public void setDiscount(int discount) {
- if (discount < 0 || discount > 100) {
- this.discount = 0;
- return;
- }
- this.discount = discount;
- }
- // 商品割引率に値を設定します。
- public void setDiscount(String discount) {
- try {
- setDiscount(Integer.parseInt(discount));
- } catch (NumberFormatException e) {
- setDiscount(0);
- e.printStackTrace();
- } catch (Exception e) {
- setDiscount(0);
- e.printStackTrace();
- }
- }
- // 商品割引率の値を取得します。
- public int getDiscount() {
- return this.discount;
- }
- // 商品の売値の値を取得します。
- public int getPrice() {
- return (int)(super.getPrice() * (1 - discount / 100.0));
- }
- }
- // 1211
- // package product;
- // セット商品クラスです。
- // ひとつまたは複数の商品を集めて、割引したものをセット商品として扱います。
- // セット商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class SetProduct extends AbstractProduct implements DiscountInterface {
- private int discount; // セット商品割引率
- private AbstractProduct[] products; // セット商品リスト
- // セット商品クラスのコンストラクター。
- public SetProduct(String code, String name, int discount, AbstractProduct[] products) {
- setCode(code);
- setName(name);
- setDiscount(discount);
- setProducts(products);
- }
- // 商品の売値の値を取得します。
- public int getPrice() {
- int price = 0;
- for (int i = 0; i < products.length; i++) {
- price += products[i].getPrice();
- }
- return (int)(price * (1 - discount / 100.0));
- }
- // 商品割引率に値を設定します。
- public void setDiscount(int discount) {
- if (discount < 10 || discount > 100) {
- this.discount = 0;
- return;
- }
- this.discount = discount;
- }
- // 商品割引率に値を設定します。
- public void setDiscount(String discount) {
- try {
- setDiscount(Integer.parseInt(discount));
- } catch (NumberFormatException e) {
- setDiscount(0);
- e.printStackTrace();
- } catch (Exception e) {
- setDiscount(0);
- e.printStackTrace();
- }
- }
- // 商品割引率の値を取得します。
- public int getDiscount() {
- return discount;
- }
- // 商品割引率に値を設定します。
- public void setProducts(AbstractProduct[] products) {
- this.products = products;
- }
- }
Exception <- E <- F
(1) void doMethod() throws E { ... }
(2) void doMethod() throws E { ... } // E 自身 (3) void doMethod() throws F { ... } // E のサブクラス (4) void doMethod() { ... } // 例外をスローしない (5) void doMethod() throws RuntimeException { ... } // 非検査例外を指定Top