java学习记录之异常

异常的分类

  1. 自己需要处理的
try, catch, finally
  1. 让别人处理的
throws

在这里插入图片描述

实际的例子

Test.java



public class Test {     
	public static void main (String  args[]) {              
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);
		int r = div(m, n);
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) {
		int r = 0;
		r= m/n;
		return r;
	}

}

执行 java Test 9 0会抛出算数异常,那怎么捕捉呢?


public class Test {     
	public static void main (String  args[]) {              
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);
		int r = div(m, n);
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) {
		int r = 0;
		try {
			r = m/n;
		} catch (ArithmeticException e) {
			System.out.println(e);
		}

		return r;
	}

}


执行结果

lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0
java.lang.ArithmeticException: / by zero
m/n=0

finally就是不管是否发生异常,都会最终执行到的语句!

加上finally看一下效果!

	public static int div(int m, int n) {
		int r = 0;
		try {
			r = m/n;
		} catch (ArithmeticException e) {
			System.out.println(e);
		} finally {
			System.out.println("this is finally!");
		}

		return r;
	}

执行的结果,9/1, 或者9/0最终都会执行finally的语句

lydia@lydia:~/sgy/java_learn/exception$ javac Test.java
lydia@lydia:~/sgy/java_learn/exception$ java Test 9 1
this is finally!
m/n=9
lydia@lydia:~/sgy/java_learn/exception$ java Test 9 0
java.lang.ArithmeticException: / by zero
this is finally!
m/n=0

怎么让别人处理

例如div函数自己本身不处理,扔给main方法


public class Test {     
	public static void main (String  args[]) {              
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);
		int r = 0;
		try {
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println(e);
		}
			
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) throws ArithmeticException {
		int r = 0;
		r = m / n;
		return r;
	}

}

main和div怎么一起处理这个异常?

现在的程序只会在div中处理这个异常!
Test.java


public class Test {     
	public static void main (String  args[]) {              
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);
		int r = 0;
		try {
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main-->"+e);
		}
			
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) throws ArithmeticException {
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
		}
		
		return r;
	}

}


执行的结果只有div会处理这个异常

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
m/n=0

需要通过手动抛出异常!

修改后的代码!

	public static int div(int m, int n) throws ArithmeticException {
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			throw e;
		}
		
		return r;
	}

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
main-->java.lang.ArithmeticException: / by zero
m/n=0

异常太多,不需要一个个列出来,捕获父类异常,或者大类的异常即可

Test.java


public class Test {     
	public static void main (String  args[]) {              
		int m = 0;		
		int n =0;

		int r = 0;
		try {
			m = Integer.parseInt(args[0]);
			n = Integer.parseInt(args[1]);
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main-->"+e);
		} catch (RuntimeException e) {
			System.out.println("main-->"+e);
		}
			
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) throws ArithmeticException {
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			throw e;
		}
		
		return r;
	}

}


执行结果

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
main-->java.lang.ArithmeticException: / by zero
m/n=0
lydia@lydia:~/sgy/java_learn/exception$ java Test 9  a
main-->java.lang.NumberFormatException: For input string: "a"
m/n=0
lydia@lydia:~/sgy/java_learn/exception$ java Test 9
main-->java.lang.ArrayIndexOutOfBoundsException: 1
m/n=0

人为的new execption

Test.java
div函数改成下面这样!

	public static int div(int m, int n)  {
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			throw new Exception("My error!");
		}
		
		return r;
	}

编译不通过,new Execption 必须处理或者抛出!

lydia@lydia:~/sgy/java_learn/exception$ javac Test.java
Test.java:27: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出
                        throw new Exception("My error!");
                        ^
1 个错误

继续更改,div函数

	public static int div(int m, int n) throws Exception{
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			throw new Exception("My error!");
		}
		
		return r;
	}

编译结果还是一样,Main函数里面也需要处理!

lydia@lydia:~/sgy/java_learn/exception$ javac Test.java
Test.java:11: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出
                        r = div(m, n);
                               ^
1 个错误

对Main方法增加 catch Exception,处理这个异常,就不会有问题


public class Test {     
	public static void main (String  args[]) {              
		int m = 0;		
		int n =0;

		int r = 0;
		try {
			m = Integer.parseInt(args[0]);
			n = Integer.parseInt(args[1]);
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main-->"+e);
		} catch (RuntimeException e) {
			System.out.println("main-->"+e);
		} catch (Exception e) {
			System.out.println("main-->"+e);
		}
			
		System.out.println("m/n=" + r);
	}

	public static int div(int m, int n) throws Exception{
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			throw new Exception("My error!");
		}
		
		return r;
	}

}

执行的结果

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
main-->java.lang.Exception: My error!
m/n=0

或者直接在div当中处理掉!
在div中增加try catch的语句,直接本方法处理!

	public static int div(int m, int n) throws Exception{
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div--->"+ e);
			try {
				throw new Exception("My error!");
			} catch (Exception e1) {
				System.out.println("div-->"+e1);
			}
			
		}
		
		return r;
	}
lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
div-->java.lang.Exception: My error!
m/n=0

PS:

  1. try或catch块中有return或throw语句,会先执行finally块,再返回来执行return或throw语句

  2. 对于“不可查异常”, 系统也会抛出它,写不写throws效果一样
    即throws ArithmeticException不写, main方法仍能够正常捕捉到!会自动抛出

	public static int div(int m, int n) throws ArithmeticException {

执行的结果

lydia@lydia:~/sgy/java_learn/exception$ java Test 9  0
div--->java.lang.ArithmeticException: / by zero
main-->java.lang.ArithmeticException: / by zero
m/n=0
发布了17 篇原创文章 · 获赞 3 · 访问量 3474

猜你喜欢

转载自blog.csdn.net/sgy1993/article/details/104573049