- <span style="font-family:KaiTi_GB2312;font-size:18px;">package Test;
- import java.lang.Exception;
- public class TestException {
- static int quotient(int x, int y) throws MyException { // 定义方法抛出异常
- if (y < 0) { // 判断参数是否小于0
- throw new MyException("除数不能是负数"); // 异常信息
- }
- return x/y; // 返回值
- }
- public static void main(String args[]) { // 主方法
- int a =3;
- int b =0;
- try { // try语句包含可能发生异常的语句
- int result = quotient(a, b); // 调用方法quotient()
- } catch (MyException e) { // 处理自定义异常
- System.out.println(e.getMessage()); // 输出异常信息
- } catch (ArithmeticException e) { // 处理ArithmeticException异常
- System.out.println("除数不能为0"); // 输出提示信息
- } catch (Exception e) { // 处理其他异常
- System.out.println("程序发生了其他的异常"); // 输出提示信息
- }
- }
- }
- class MyException extends Exception { // 创建自定义异常类
- String message; // 定义String类型变量
- public MyException(String ErrorMessagr) { // 父类方法
- message = ErrorMessagr;
- }
- public String getMessage() { // 覆盖getMessage()方法
- return message;
- }
- } </span>
要注意的是,throw 抛出的只能够是可抛出类Throwable 或者其子类的实例对象。
要注意的是,throw 抛出的只能够是可抛出类Throwable 或者其子类的实例对象
(1)、try-catch
实例:
- public class TestException {
- public static void main(String[] args) {
- int[] intArray = new int[3];
- try {
- for (int i = 0; i <= intArray.length; i++) {
- intArray[i] = i;
- System.out.println("intArray[" + i + "] = " + intArray[i]);
- System.out.println("intArray[" + i + "]模 " + (i - 2) + "的值: "
- + intArray[i] % (i - 2));
- }
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.println("intArray数组下标越界异常。");
- } catch (ArithmeticException e) {
- System.out.println("除数为0异常。");
- }
- System.out.println("程序正常结束。");
- }
- }
上面的代码中捕获两个错误:除数为0和数组下标越界异常。运行结果