Java异常处理

来源:互联网 发布:stm32f103tbu6编程 编辑:程序博客网 时间:2024/06/06 21:01

throws 关键字

package com.hotmail.henrytien;public class ExceptionTest {    public static void tell(int i,int j) throws ArithmeticException{        int temp = 0;        temp = i/j;        System.out.println(temp);    }    public static void main(String[] args) {        // TODO Auto-generated method stub        try {            tell(10,0);        } catch (Exception e) {            System.out.println(e);        }    }}

  • 自定义异常
package com.hotmail.henrytien;class MyException extends Exception{     public MyException(String msg) {         super(msg);     }}public class ExceptionDemo {    public static void main(String[] args) {    try {             throw new MyException("自定义异常");    } catch (MyException e) {        System.out.println(e);    }    }}
0 0
原创粉丝点击