java - 基础 - 报错

异常 / 错误

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{

    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println(i);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">try start
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Exception appeared
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">错误类型:java.lang.ArithmeticException: / by zero</span>

    <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
        System.out.println(</span>"try start"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">int</span> i = 1<span style="color: rgba(0, 0, 0, 1)">;
        System.out.println(</span>"try end"<span style="color: rgba(0, 0, 0, 1)">);
    }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(Exception e){
        System.out.println(</span>"错误:" +<span style="color: rgba(0, 0, 0, 1)"> e);
    }</span><span style="color: rgba(0, 0, 255, 1)">finally</span>{ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">不论是否报错都会执行,一般用来提示进度或者释放内存</span>
        System.out.println("try finish"<span style="color: rgba(0, 0, 0, 1)">);
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">try start
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">try end
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">try finish</span>
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
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">总结: fianally总会在try-catch之后执行,return会在try - catch - finally之后执行,如果finanlly中有return执行finally中的(覆盖了?)\


    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Throws</span>
    <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        et.finallyTest3();
    }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(Exception e){
        System.out.println(e);
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">finallyTest3 开始运行
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">java.lang.ArithmeticException: / by zero
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">throws可以把异常向上抛出,交给运行这个方法执行的方法处理。
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">接受这个异常的方法还可以继续向上抛出异常,利用这个原理可以把异常集中处理。</span>
    et.myException();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">myException 开始运行
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">自己定义的异常
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Exception in thread "main" error.MyException
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    at error.ErrorTest.myException(ErrorTest.java:108)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    at error.ErrorTest.main(ErrorTest.java:62)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">要是之前有其他异常被抛出,会导致异常的抛出时间有时候会很诡异。。。</span>

}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String finallyTest(){
    String s </span>= "aaa"<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
        System.out.println(</span>"Finally测试:try执行了"<span style="color: rgba(0, 0, 0, 1)">);
        s </span>= "bbb"<span style="color: rgba(0, 0, 0, 1)">;
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> "return1: " +<span style="color: rgba(0, 0, 0, 1)"> s;
    }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
        System.out.println(</span>"Finally测试:finally执行了"<span style="color: rgba(0, 0, 0, 1)">);
        s </span>= "ccc"<span style="color: rgba(0, 0, 0, 1)">;
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> "return2: " +<span style="color: rgba(0, 0, 0, 1)"> s;
    }
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String finallyTest2(){
    </span><span style="color: rgba(0, 0, 255, 1)">int</span> i = 111<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
        System.out.println(</span>"Finally测试:try执行了"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">i = 1/0;</span>
        <span style="color: rgba(0, 0, 255, 1)">return</span> "return1: " +<span style="color: rgba(0, 0, 0, 1)"> i;
    }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(Exception e){
        System.out.println(</span>"Finally测试:catch执行了"<span style="color: rgba(0, 0, 0, 1)">);
        i </span>= 222<span style="color: rgba(0, 0, 0, 1)">;
    }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
        System.out.println(</span>"Finally测试:finally执行了"<span style="color: rgba(0, 0, 0, 1)">);
        i </span>= 333<span style="color: rgba(0, 0, 0, 1)">;
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> "return2: " +<span style="color: rgba(0, 0, 0, 1)"> i;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> String finallyTest3() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception{

    System.out.println(</span>"finallyTest3 开始运行"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">int</span> i = 1/0<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> "return: " +<span style="color: rgba(0, 0, 0, 1)"> i;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> myException(){
    System.out.println(</span>"myException 开始运行"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">int</span> a = 1<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">int</span> b = 2<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(a &lt;<span style="color: rgba(0, 0, 0, 1)"> b){
        </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MyException();
    }
}

}

 

 

package error;

public class MyException extends RuntimeException {
public MyException(){
System.out.println(
"自己定义的异常");
}
}