标签:list string 异常类 应用 业务需求 return 辅助 ros normal
1 package com.exception.demo02; 2 3 //自定义的异常类:继承Exception 4 public class MyException extends Exception { 5 6 //传递数字>10; 7 private int detail; 8 9 public MyException(int a) { 10 this.detail = a; 11 } 12 13 //toString:异常的打印信息 14 @Override 15 public String toString() { 16 return "MyException{" + detail + ‘}‘; 17 } 18 }
1 package com.exception.demo02; 2 3 public class Test { 4 5 //可能会存在异常的方法 6 7 static void test(int a) throws MyException { 8 9 System.out.println("传递的参数为:" + a); 10 if (a > 10) { 11 throw new MyException(a);//抛出 12 } 13 14 System.out.println("OK"); 15 } 16 17 18 public static void main(String[] args) { 19 try { 20 test(11); 21 } catch (MyException e) { 22 System.out.println("MyException=>" + e); 23 } 24 } 25 26 } 27 28 结果: 29 传递的参数为:11 30 MyException=>MyException{11}
标签:list string 异常类 应用 业务需求 return 辅助 ros normal
原文地址:https://www.cnblogs.com/duanfu/p/12222702.html