try...catch语句

程序的异常:Throwable

严重问题Error我们不处理,这种问题一般都是很严重的,比如内存溢出

问题Exception

编译期问题不是RuntimeException的异常必须进行处理,如果不处理,编译就不能通过

运行期问题RuntimeException这种问题是代码不够严谨,需要修正代码

/*
* 如何处理异常?
*
* A:try...catch...finally
* B:throws
*
* try {
语句块//执行该语句块,可能会发生异常
} catch (异常类型 e) {
异常发生时要执行的语句块
} catch (异常类型 e) {
异常发生时要执行的语句块
} finally {finally {
无论异常发生与否,都要执行的语句块
}
*
*
* 注意:
* A:try里面的代码越少越好
* B:catch{}花括号里面必须有内容,哪怕是给出一个简单的提示
* C:异常类型能明确的进来明确,不要用大的异常类型来处理
* D:平级关系的异常类型先后顺序无关系,但是如果出现了继承关系的异常类型,超类必须在最后
* */

/*
 * 如何处理异常?
 * 
 * A:try...catch...finally
 * B:throws
 * 
 * 		try {
			语句块//执行该语句块,可能会发生异常
		} catch (异常类型 e) {
			异常发生时要执行的语句块
		} catch (异常类型 e) {
			异常发生时要执行的语句块
		} finally {finally {
			无论异常发生与否,都要执行的语句块
		}
 * 
 * 
 * 注意:
 * A:try里面的代码越少越好
 * B:catch{}花括号里面必须有内容,哪怕是给出一个简单的提示
 * C:异常类型能明确的进来明确,不要用大的异常类型来处理
 * D:平级关系的异常类型先后顺序无关系,但是如果出现了继承关系的异常类型,超类必须在最后
 * */

public class IntegerDemo {
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		method();
	}

	public static void method() {
		int a = 10;
		int b = 0;
		int arr[] = { 1, 2, 3 };

		try {
			System.out.println(a / b);
			System.out.println(arr[3]);
		} catch (ArithmeticException e) {
			System.out.println("除数不能为0");
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("数组越界");
		} catch (Exception e) {
			System.out.println("出错了");
		} finally {

		}

		System.out.println("OVER");
	}
}

/*
* JDK7新的异常处理方案
* try {
*
} catch (异常类型1 | 异常类型2 变量) {

} finally {

}
*
* 注意:这个方法虽然简洁,但是也不够好
* A:处理方式是一致的。(实际开发中,好多时候可能就是针对同类型的问题,给出同一个处理)
* B:多个异常之间必须是平级关系
* */

/*
 * JDK7新的异常处理方案
 * 		try {
 * 
		} catch (异常类型1 | 异常类型2 变量) {
		
		} finally {

		}
 * 
 * 注意:这个方法虽然简洁,但是也不够好
 * A:处理方式是一致的。(实际开发中,好多时候可能就是针对同类型的问题,给出同一个处理)
 * B:多个异常之间必须是平级关系
 * */

public class IntegerDemo {
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		method();
	}

	public static void method() {
		int a = 10;
		int b = 0;
		int arr[] = { 1, 2, 3 };

		try {
			System.out.println(a / b);
			System.out.println(arr[3]);
		} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
			System.out.println("出错了");
		} finally {

		}

		System.out.println("OVER");
	}
}

/*
* 面试题:
* 如果catch里面有return语句,请问finally里面的代码还会执行吗?如果执行,请问在return前,还是return后?
*
* 执行。前。
* 准确的说,应该是在中间
* */

/*
 * 面试题:
 * 如果catch里面有return语句,请问finally里面的代码还会执行吗?如果执行,请问在return前,还是return后?
 * 
 * 执行。前。
 * 准确的说,应该是在中间
 * */

public class IntegerDemo {
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		System.out.println(getInt());
	}

	public static int getInt() {
		System.out.println("beginning of getInt");
		int a = 10;

		try {
			System.out.println("beginning of try");
			System.out.println(a / 0);
			a = 20;
			System.out.println("end of try");
		} catch (ArithmeticException e) {
			System.out.println("beginning of catch");
			a = 30;
			System.out.println("end of catch");
			return a;
			/*
			 * return a;在程序执行到这一步的时候,这里不是return a;而是return 30;这个返回路径就形成了
			 * 但是,程序发现后面还有finally,所以继承执行finally语句块,a = 40;
			 * 再次回到以前的返回路径,继续执行return 30;
			 */
		} finally {
			System.out.println("beginning of finally");
			a = 40;
			System.out.println("end of finally");
		}

		System.out.println("end of getInt");
		return a;
	}
}

/*
* 异常注意事项:
* A:子类重写父类方法时,子类的方法必须提出相同的异常或父类异常的子类。(父类坏了,子类不能比父类更坏)
* B:如果父类抛出了多个异常,子类重写父类时,只能抛出相同的异常或者是子类,子类不能抛出父类没有的异常
* C:如果被重写的方法没有异常抛出,那么子类的方法绝对不可以抛出异常。如果子类方法内有异常抛出,那么子类只能try,不能throws
* */

/*
 * 异常注意事项:
 * A:子类重写父类方法时,子类的方法必须提出相同的异常或父类异常的子类。(父类坏了,子类不能比父类更坏)
 * B:如果父类抛出了多个异常,子类重写父类时,只能抛出相同的异常或者是子类,子类不能抛出父类没有的异常
 * C:如果被重写的方法没有异常抛出,那么子类的方法绝对不可以抛出异常。如果子类方法内有异常抛出,那么子类只能try,不能throws
 * */

public class IntegerDemo {
	public static void main(String[] args) {
	}
}

class Fu {
	public void show() throws Exception {
	}
}

class Zi extends Fu {
	public void show() throws ArithmeticException {
	}
}
原文地址:https://www.cnblogs.com/denggelin/p/6295381.html