1 package gys; 2 3 public class ExpetionTest { 4 public static void main(String[] args) { 5 NumberCheck n=new NumberCheck(); 6 try { 7 System.out.println(n.division(1,-3)); 8 } catch (ZerpSubException e) { 9 System.out.println(e.getMessage()); 10 } catch (ZeroException e) { 11 System.out.println(e.getMessage()); 12 } 13 catch(Exception ex){ 14 System.out.println(ex.getMessage()); 15 } 16 } 17 } 18 class ZeroException extends Exception{ 19 public ZeroException(String msg){ 20 super(msg); 21 } 22 } 23 class ZerpSubException extends Exception{ 24 public ZerpSubException(String msg){ 25 super(msg); 26 } 27 } 28 class NumberCheck{ 29 //除法 30 public int division(int x,int y) throws ZeroException,ZerpSubException{ 31 if(y<0){ 32 throw new ZerpSubException("除数不能为负数"); 33 } 34 if(y==0){ 35 throw new ZeroException("除数不能为0"); 36 } 37 int m=x/y; 38 return m; 39 } 40 }