import java.util.Scanner; public class MyExceptionTest { public static void check(Square A) throws WrongException { if(A.getChang()<=0 && A.getKuan()<=0){ throw new WrongException("长<=0,不合法, 宽<=0,也不合法"); } if(A.getKuan()<=0){ throw new WrongException("宽<=0,不合法"); } if(A.getChang()<=0){ throw new WrongException("长<=0,不合法"); } A.CalcuArea(); } public static void main(String[] args) { // TODO Auto-generated method stub Square A=new Square(); Scanner in =new Scanner(System.in); System.out.println("请输入矩形参数:\n"+"长: 宽:\n"); int Chang=in.nextInt(); A.setChang(Chang); int kuan=in.nextInt(); A.setKuan(kuan); try{ check(A); System.out.println(A.toString()); }catch(WrongException e){ System.out.println("报错啦\n"+e.getMessage()); e.toString(); e.printStackTrace(); }finally{ System.out.println("谢谢使用,再见!"); } } } class Square { private int Chang,Kuan; private int Area; public int getChang() { return Chang; } public String toString() { return "矩形 [长=" + Chang + ", 宽=" + Kuan + ", 面积=" + Area + "]"; } public void setChang(int chang) { Chang = chang; } public int getKuan() { return Kuan; } public void setKuan(int kuan) { Kuan = kuan; } public void CalcuArea() { Area=Chang*Kuan; } } class WrongException extends Exception { //定义一个Message String Message; //构造方法,设置Message public WrongException(String Message) { this.Message = Message; } //输出Message public String getMessage() { return Message; } }
public class Test1 { public static void main(String[] args) { // TODO Auto-generated method stub int a = 100, b = 0, c; try{ c = a / b; System.out.println("c=" + c); }catch(ArithmeticException e){ System.out.println("catched!"); System.out.println("catch ArithmeticException message:"+ e.getMessage()); System.out.println("catch ArithmeticException toSting():" + e.toString()); e.printStackTrace();//打印出现错误的地方 }finally{ System.out.println("finally!"); } } }
public class Test4 { public static void main(String[] args) { int a = 100, b = 2, c = 0; int[] x = { 10, 20, 30, 40, 50, 60, 70 }; // 如果try写在for循环外面,异常后就跳出,将不再进入循环 for (int i = 0; i <= 10; i++) { System.out.println("c=" + c); try { c = a / b--; System.out.println("x[" + i + "]=" + x[i]); } catch (ArithmeticException e) { System.out.println("catched ArithmeticException message:" + e.getMessage()); System.out.println("catched ArithmeticException toSting():" + e.toString()); e.printStackTrace(); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("catched ArrayIndexOutOfBoundsException!"); } finally { System.out.println("finally!"); } } } }
c=0 x[0]=10 finally! c=50 x[1]=20 finally! c=100 catched ArithmeticException message:/ by zero catched ArithmeticException toSting():java.lang.ArithmeticException: / by zero java.lang.ArithmeticException: / by zero at Test4.main(Test4.java:11) finally! c=100 x[3]=40 finally! c=-100 x[4]=50 finally! c=-50 x[5]=60 finally! c=-33 x[6]=70 finally! c=-25 catched ArrayIndexOutOfBoundsException! finally! c=-20 catched ArrayIndexOutOfBoundsException! finally! c=-16 catched ArrayIndexOutOfBoundsException! finally! c=-14 catched ArrayIndexOutOfBoundsException! finally!
//使用 throw 语句抛出异常、使用 throws 子句抛弃异常 public class TestThrow1 { static void throwProcess(Object o) throws NullPointerException{// throws NullPointerException可不写 if(o==null){ throw new NullPointerException("空指针异常");//手动写抛出异常 } //若抛出异常则不再执行函数下面的语句 System.out.println(o+"哈哈呵呵"); } public static void main(String[] args) { Object p=null; try{ throwProcess(p); }catch(NullPointerException e){ System.out.println("捕获:" + e); } } }