java异常处理机制

1、异常简介

Java异常的分类和类的结构图 ![](https://img2018.cnblogs.com/blog/1373276/201905/1373276-20190515170059259-1323033007.png)

Throwable是所有异常类的顶级父类,下面又分为Error和Exception

Error:错误,表示JVM本身的错误,错误不能被程序处理,表示运行中比较严重的问题
Exception:异常,是程序本身可以处理的异常,表示程序运行期间不希望被发生的事件,可以被程序处理。

异常的分类

运行时异常 RuntimeException类及其子类的异常,是由程序逻辑错误引导起的,Java编译器在编译期间不会检查这些异常,只有程序运行时才会出现。这些异常在程序中可以处理,也可以不处理,
非运行时异常,是RuntimeException以外的异常,这些异常必须处理,如果不处理,程序就不能编译通过

2、try,catch,finally执行顺序

2.1、try,catch有返回值,finally无返回值

public class TryCatchFinally1 {

	public static void main(String[] args) {
		System.out.println(test());
	}

	public static String test() {
		String t = "";
		try {
			t = "try";
			return t;
		} catch (Exception e) {
			t = "catch";
			return t;
		} finally {
			t = "finally";
		}
	}
}

运行结果:

try

2.2、try,catch,finally都有返回值

public class TryCatchFinally2 {

	public static void main(String[] args) {
		System.out.println(test());
	}

	public static String test() {
		String t = "";
		try {
			t = "try";
			return t;
		} catch (Exception e) {
			t = "catch";
			return t;
		} finally {
			t = "finally";
			return t;
		}
	}
}

运行结果:

finally

2.3、try抛出异常、finally无返回值

public class TryCatchFinally3 {

	public static void main(String[] args) {
		System.out.println(test());
	}

	public static String test() {
		String t = "";
		try {
			t = "try";
			Integer.parseInt(null);
			return t;
		} catch (Exception e) {
			t = "catch";
			return t;
		} finally {
			t = "finally";
		}
	}
}

运行结果:

catch

2.4、try抛出异常、finally有返回值

public class TryCatchFinally4 {

	public static void main(String[] args) {
		System.out.println(test());
	}

	public static String test() {
		String t = "";
		try {
			t = "try";
			Integer.parseInt(null);
			return t;
		} catch (Exception e) {
			t = "catch";
			return t;
		} finally {
			t = "finally";
			return t;
		}
	}
}

运行结果:

finally

2.5、try、catch抛出异常、finally无返回值

public class TryCatchFinally5 {

	public static void main(String[] args) {
		System.out.println(test());
	}

	public static String test() {
		String t = "";
		try {
			t = "try";
			Integer.parseInt(null);
			return t;
		} catch (Exception e) {
			t = "catch";
			Integer.parseInt(null);
			return t;
		} finally {
			t = "finally";
		}
	}
}

运行结果:

Exception in thread "main" java.lang.NumberFormatException: null

2.6、try、catch抛出异常、finally有返回值

public class TryCatchFinally6 {

	public static void main(String[] args) {
		System.out.println(test());
	}

	public static String test() {
		String t = "";
		try {
			t = "try";
			Integer.parseInt(null);
			return t;
		} catch (Exception e) {
			t = "catch";
			Integer.parseInt(null);
			return t;
		} finally {
			t = "finally";
			return t;
		}
	}
}

运行结果:

finally

2.7、try抛出异常,catch其他异常,finally无返回值

public class TryCatchFinally7 {

	public static void main(String[] args) {
		System.out.println(test());
	}

	public static String test() {
		String t = "";
		try {
			t = "try";
			Integer.parseInt(null);
			return t;
		} catch (NullPointerException e) {
			t = "catch";
			return t;
		} finally {
			t = "finally";
		}
	}
}

运行结果:

Exception in thread "main" java.lang.NumberFormatException: null

2.8、try抛出异常,catch其他异常,finally有返回值

public class TryCatchFinally8 {

	public static void main(String[] args) {
		System.out.println(test());
	}

	public static String test() {
		String t = "";
		try {
			t = "try";
			Integer.parseInt(null);
			return t;
		} catch (NullPointerException e) {
			t = "catch";
			return t;
		} finally {
			t = "finally";
			return t;
		}
	}
}

运行结果:

finally

2.9、finally有异常

public class TryCatchFinally8 {

	public static void main(String[] args) {
		System.out.println(test());
	}

	public static String test() {
		String t = "";
		try {
			t = "try";
			Integer.parseInt(null);
			return t;
		} catch (NullPointerException e) {
			t = "catch";
			return t;
		} finally {
			t = "finally";
			return t;
		}
	}
}

运行结果:

Exception in thread "main" java.lang.NullPointerException

总结:

1、try、catch、finally语句中,如果只有try语句有return返回值,此后在catch、finally中对变量做任何的修改,都不影响try中return的返回值。
2、try、catch中有返回值,而try中抛出的异常恰好与catch中的异常匹配,则返回catch中的return值。
3、如果finally块中有return 语句,则try或catch中的返回语句被忽略。
4、如果finally块中抛出异常,则整个try、catch、finally块中抛出异常.并且没有返回值。

参考链接:https://blog.csdn.net/mxd446814583/article/details/80355572

posted @ 2019-06-16 15:26  风中的蜗牛  阅读(191)  评论(0编辑  收藏  举报