Java 异常整合练习
1 package com.bytezero.throwable2; 2 3 /** 4 * 5 * @Description 异常练习 6 * @author Bytezero·zhenglei! Email:420498246@qq.com 7 * @version 8 * @date 上午9:01:38 9 * @ 10 * 11 */ 12 public class EcmDef { 13 public static void main(String[] args) { 14 15 try { 16 int i= Integer.parseInt(args[0]); 17 int j= Integer.parseInt(args[1]); 18 19 int result =ecm(i,j); 20 21 System.out.println(result); 22 23 }catch(NumberFormatException e) { 24 System.out.println("数据类型不一致"); 25 26 }catch(ArrayIndexOutOfBoundsException e) { 27 System.out.println("缺少命令行参数"); 28 }catch(ArithmeticException e) { 29 System.out.println("除0"); 30 }catch(EcDef e) { 31 System.out.println(e.getMessage()); 32 33 } 34 35 } 36 37 public static int ecm(int i ,int j) throws EcDef { 38 if(i <0 || j<0) { 39 40 throw new EcDef("分子或分母为负数"); 41 } 42 return i/j; 43 } 44 }
1 package com.bytezero.throwable2; 2 3 //自定义异常类 4 public class EcDef extends Exception { 5 6 static final long serialVersionUID = -3387516124229948L; 7 8 public EcDef() { 9 10 } 11 public EcDef(String msg) { 12 13 super(msg); 14 } 15 16 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15388599.html