一、try catch : 捕获异常
try {
// 可能出现异常的代码
} catch (Exception e) {
// 异常处理的相关代码,如: getMessage() 、pr intStackTrace ()
} finally {
//无论是否出现异常,都需执行的代码结构,常用于释放资源。
}
二、常见 try catch 异常结构
- try和catch都不能单独使用,必须连用
- 多重catch,遵循从子(小)到父(大)
三、try catch demo
模拟Error中的堆溢出和栈溢出
public class ErrorDemo {
public static void main(String[] args) {
//堆溢出
// ArrayList list = new ArrayList();
// while(true) {
// list.add("你好");
// }
//栈溢出:递归
test();
}
private static void test() {
test();
System.out.println(123);
}
}
/* try...catch...finally的基本使用
* 1、运行时异常是不推荐你去进行处理的;
* 2、这里的算术运算是RuntimeException异常,按道理
* 来说,是不需要我们进行处理的,但是为了方便我们进行
* try..catch的使用演示,使用我们在这里对它进行try
* catch的操作;
* 3、catch的操作
* a、catch后面放的是你确定会出现的异常类对象,以及对应的操作;
* b、我们往往会在用户真实环境中的时候,在最后面加一个catch让
* 其来拦截一些我们无法预料的异常,为的就是不让程序在用户使用
* 过程中莫名其妙的终止;
* c、如果说方法里面有可能出现多个异常的话,那么这些异常我们都
* 分别进行catch就行了,只要不存在父类和子类关系的话,他们所出现
* 的顺序无所谓;
* 4、finally的操作
* 不管怎么样都会执行的代码,一般用来放关闭资源的操作
*
*/
public class TryCatchDemo {
public static void main(String[] args) {
//模仿一个运行时异常的出现
/*
* try { "abc".charAt(10); }catch(Exception e) {
* System.out.println("这里会出现一个字符串下标越界异常"); } System.out.println("这里是继续运行的代码");
*/
int a = 10;
int b = 0;
int result = 0;
try {
result = chufa(a,b);
} catch (ArithmeticException e) {
//ArithmeticException e = new ArithmeticException();
//catch代码块里面一般用来进行错误信息的打印或者是把错误信息写入到日志系统里面
//printStackTrace():把完整的错误信息给进行输出,注意,这里是数组输出内容,而且是
//另外一个线程进行控制的,所以会导致输出的内容和后面main线程的输出内容混杂在一起,这是正常的
// e.printStackTrace();
//e.toString():把异常信息和错误信息进行输出
// System.out.println(e.toString());
//e.getMessage():你手动所传入的信息进行输出
System.out.println(e.getMessage());
//如果你要写入到日志系统的话,那么此处需要File类的使用以及IO流的使用
} catch (Exception e) {
//这里是进行保险的操作的,因为万一我们没有把所有的异常信息给考虑到,这里出现了未知的其他
//异常的话,我们不希望程序因此停止掉,我们希望捕获掉异常,发给程序员去看
} finally {
System.out.println("不管如何,这里都会执行");
}
System.out.println("result的值是:"+result);
}
private static int chufa(int a, int b) {
int result = a/b;
return result;
}
}
四、throw 抛出异常
1、 throw的相关解释
➢throw的意思是抛出(不处理异常,调用者来处理)
,后面接具体的导常对象;
➢throw放在代码块内部,用来抛出一个异常对象,将这个异常对象传递到调用者处,并结束当前方法的执行;
➢throw后面如果接运行时异常,一旦 该代码执行,那么程序会停止,如果后面接的是检查时异常,就会要求你处理该异常,并且该代码执行了,程序不会停止;
注意:一般来说, 我们使用throw抛出的异 常都是一种提示性的语句, 它不会让程序结束,它代表告诉方法的调用者,一旦你不满足某个预订条件的时候,我这里就会出现一种错误, 请调用者进行更改;
2、throw的语法
throw new异常类名(参数);
例如:
throw new NullPointerException(“要访问的arr数组不存在”);
throw new ArrayIndexOutOfBoundsException(“该索引在数组中不存在,已超出范围”);
3、throw demo
throw的使用
1、此处演示的throw后面的异常对象是一个RuntimeException,所以JVM
并没有强行要求我们对该异常进行处理(try…catch)
2、如果你的throw后面接的是一个检查时异常的时候,那么此时JVM就要求
你必须要处理该异常
a、我出现就处理;—>注意,你抛出去,又捕获解决了,没有任何的含义
b、我自己不处理,谁来使用我这个方法,谁处理;
public class ThrowDemo {
public static void main(String[] args) throws Exception {
int [] arr = {2,8,5,4};
int index = 4;
int element = getElement(arr,index);
System.out.println(element);
System.out.println("over");
}
private static int getElement(int[] arr, int index) throws Exception {
if(index<0||index>=arr.length) {
// throw new ArrayIndexOutOfBoundsException("兄弟,你的数组下标越界了,越界的值是:"+index);
throw new Exception("哥们,你的操作违规了,违规的原因是数组越界,下标是:"+index);
}
int element = arr[index];
return element;
}
}
五、throws声明异常
1、throws的相关解释
➢throws的意思是声明,后面接具体的异常类;
➢throws放在方法结构尾部,用来告诉方法的调用者,这个方法里面有个异常,需要调用者处理该异常;
➢throws必须和throw配套使用
,当throw的是一个运行时异常时,JVM不会强制要求你在对应方法上加上throws,
但是如果throw了一个检查时异常时,JVM就要求你必须要在方法结构上声明该方法里会出现的异常类型;
➢throws后面可以接多个异常类,中间用逗号隔开;
注意:关键字throws运用于方法声明之.上,用于表示当前方法不处理异常,而是提醒该方法的调用者来处理异常
(抛出异常)。
2、throws的语法
修饰符返回值类型方法名(参数) throws异常类名1,异常类名2…{ }
例如:
public static void test1(String str) throws FileNotFoundException {}
public static void test2(String str) throws FileNotFoundException, IOException {}
3、 throws demo
public class ThrowsDemo {
public static void main(String[] args) {
try {
test();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
System.out.println("文件读取完毕!");
}
public static void test() throws FileNotFoundException {
//1、方法的调用者已经把该异常给处理掉了,那么此时,该方法的调用者就不会再发现
//该异常了!
// try {
// read("a.txt");
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//2、方法内部无法解决该异常,所以继续声明出来,让调用者来进行解决
read("b.txt");
}
private static void read(String str) throws FileNotFoundException {
if(!"a.txt".equals(str)) {
throw new FileNotFoundException("你要读取的文件不存在!");
}
System.out.println("你读取到了红楼梦文件!");
}
}
六、运行时和检查时异常的使用区别
➢运行时异常是属于JVM不检查是否出现的异常,也就是说,JVM会认为需要它爆发出来把程序停止
,所以不需要你进行try…catch和throws;
➢检查时异常是属于我们为了进行提示、日志记录作用的,所以这类异常JVM是要求你必须处理的,你可以选择throws来声明出去,让调用者来处理,也可以选择try…catch掉,自己来处理,反正得处理,不处理,就会编译出错
;