异常/错误
java中处理异常的类:Throwable
错误(Error):一般是物理问题,jvm本身出现错误,无法处理。 内存溢出等
异常(Exception):指令产生的不符合规定的错误。 可以抛出异常或者通过程序指令进行处理
Exception的分类:
运行时异常:非检查异常(编译时不会报错)
编译时异常:检查异常(不修改不能通过编译)
Error:
StackOverflowError:栈内存溢出。
栈内存一般用于变量存储,临时方法执行,基本变量值,引用变量地址存储等。
递归没写停止条件。
OutOfMemoryError:堆内存溢出。
堆内存一般用于存储引用变量内容。
对象过多或者过大。
Exception:
ClassCastException 造型异常
没有继承关系的两个类进行强制转换。
InputMissMatchException 输入不匹配
nextInt() 输入了 字符串一类的
NumberFormateExcep 数字格式化异常
String转int时: Integer.pasrseInt("aaa"); 输入与要求不匹配
ArrayIndexOutOfBoundsException 数组越界
int[] i = new int[3]; i[4] = 0;
NegativeArraySizeException 数组长度为负数
int[] i = new int[-3];
NullPointerException 空指针
操作没有分配空间的对象 Person p = null; p.setName("aaa");
ArithmeticException 算术异常
int a = 1/0;
Exception的处理: 可以添加try throws进行处理
try{代码}catch(异常的类型 变量){对捕获的异常进行处理}
try{代码}fanally{如果try不能正常运行,运行fanally的代码}
try中的代码运行到异常时停止,然后接着运行catch或者fanally
Throws
放在方法后面,抛出异常,可以多个,用逗号隔开
package error;
public class ErrorTest {
public static void main(String[] args){
try{
System.out.println("try start");
int i = 1/0; //数学错误异常
int[] iArr = new int[]{1,2,3};
iArr[4] = 0; //数组越界异常
System.out.println("try end");
}catch(ArrayIndexOutOfBoundsException e){ //只有出现ArrayIndexOutOfBoundsException错误时才会运行这个catch,否则不执行,catch可以设置多个处理不同的错误。 catch(Exception e) 可以捕获所有异常。
System.out.println("ArrayIndexOutOfBoundsException appeared");
System.out.println("数组越界:" + e);
}catch(ArithmeticException e){
System.out.println("ArithmeticException appeared");//因为运行到int i = 1/0 时,程序就抛出异常并且终止try了。所以只会执行这一个catch
System.out.println("数学错误:" + e);
}catch(Exception e){
//其他异常,当之前的异常不存在,但是还有其他异常时执行,一般用来防止遗漏,catch的顺序一般是从小异常到大异常
System.out.println("数学错误:" + e);
}finally{
}
//System.out.println(i);
//try start
//Exception appeared
//错误类型:java.lang.ArithmeticException: / by zero
try{
System.out.println("try start");
int i = 1;
System.out.println("try end");
}catch(Exception e){
System.out.println("错误:" + e);
}finally{ //不论是否报错都会执行,一般用来提示进度或者释放内存
System.out.println("try finish");
}
//try start
//try end
//try finish
ErrorTest et = new ErrorTest();
System.out.println(et.finallyTest());
//Finally测试:try执行了
//Finally测试:finally执行了 try中有返回,finally仍然会执行
//return2: ccc 执行的是finally中的return
System.out.println(et.finallyTest2());
//Finally测试:try执行了
//Finally测试:finally执行了 fianlly仍然执行
//return1: 111 执行的是try中的return
//总结: fianally总会在try-catch之后执行,return会在try - catch - finally之后执行,如果finanlly中有return执行finally中的(覆盖了?)\
//Throws
try {
et.finallyTest3();
}catch(Exception e){
System.out.println(e);
}
//finallyTest3 开始运行
//java.lang.ArithmeticException: / by zero
//throws可以把异常向上抛出,交给运行这个方法执行的方法处理。
//接受这个异常的方法还可以继续向上抛出异常,利用这个原理可以把异常集中处理。
et.myException();
//myException 开始运行
//自己定义的异常
//Exception in thread "main" error.MyException
// at error.ErrorTest.myException(ErrorTest.java:108)
// at error.ErrorTest.main(ErrorTest.java:62)
//要是之前有其他异常被抛出,会导致异常的抛出时间有时候会很诡异。。。
}
public String finallyTest(){
String s = "aaa";
try{
System.out.println("Finally测试:try执行了");
s = "bbb";
return "return1: " + s;
}finally {
System.out.println("Finally测试:finally执行了");
s = "ccc";
return "return2: " + s;
}
}
public String finallyTest2(){
int i = 111;
try{
System.out.println("Finally测试:try执行了");
//i = 1/0;
return "return1: " + i;
}catch(Exception e){
System.out.println("Finally测试:catch执行了");
i = 222;
}finally {
System.out.println("Finally测试:finally执行了");
i = 333;
}
return "return2: " + i;
}
public String finallyTest3() throws Exception{
System.out.println("finallyTest3 开始运行");
int i = 1/0;
return "return: " + i;
}
public void myException(){
System.out.println("myException 开始运行");
int a = 1;
int b = 2;
if(a < b){
throw new MyException();
}
}
}
package error;
public class MyException extends RuntimeException {
public MyException(){
System.out.println("自己定义的异常");
}
}
















