java异常

来源:互联网 发布:苏州淘宝模特招聘 编辑:程序博客网 时间:2024/06/03 13:24
java中处理异常的两种方法:
1.try catch 语句捕获并抛出异常
2.在方法的声明处通过throws抛出异常

java虚拟机的方法调用栈
public class Main
{
    public static void methodA(){};
    public static void methodB(){methodA()};
    public static void main(String[] args)
    {
        methodB();
    }
}


methodA()  栈顶在,正在执行的方法
methodB()
main()     栈底



每个线程都有一个独立的方法调用栈。对于。当一个新方法被调用时,Java
虚拟机把描述该方法的栈结构置入栈顶,位于栈顶的方法为正在执行的方法。


当一个方法正常执行时,Jav虚拟机会从调用栈中弹出该方法的栈结构,然后继续处理前一个方法。如果在执行该方法的过程中抛出异常,
则java虚拟机必须找到能捕获该异常的catch代码块。它首先查看当前方法是否存在这样的catch代码块,如果有,就执行catch语句,
否则,Java虚拟机会从方法调用栈弹出该方法的栈结构,继续从前一个方法寻找catch语句

当Java虚拟机追溯到调用栈的底部时,如果没有处理该异常的代码块,将会
1.则调用异常对象的printStackTrace方法,打印来自方法调用栈的异常信息
2.如果该线程不是主线程,那么终止这个线程,其他线程正常运行。如果该线程的是主线程(即方法调用栈的底部是main()方法),那么整个应用程序终止

throws字句:如果一个方法可能会出现异常,但没有能力处理这种异常,可以在方法声明中使用throws字句来生命抛出异常
throw语句后面不允许紧跟其他语句,因为这些语句永远不会执行,

异常运行的运行过程,如果程序中还包含return和System.exit()语句,就会使流程变得很复杂
1.finally语句不被执行的唯一情况是先执行了用于终止程序的System.exit()方法,用于终止当前的Java虚拟机进程,
方法定义如下:
public static void exit(int status)
exit()方法的参数status表示程序终止时的状态码,按照编程的惯例,0表示正常终止,非0数字表示一场终止
2.return 语句用于退出该方法.在执行try或catch语句中的return语句时,假如有finally代码块,会先执行finally代码块,
public class WithReturn
{
    public static int methodA(int money) throws OutOfException
    {
        if(money < 0)
        {
            throw new OutOfException("no money");
        }
        return money;
    }
    public static int methodB(int money)
    {
        System.out.println("hello");
        try{
            int result = methodA(money);
            return result;
        }catch(OutOfException e)
        {
            System.out.println(e.getMessage());
            return -100;
        }finally
        {
            System.out.println("finally");
        }
    }
    public static void main(String[] args)
    {
        System.out.println(methodB(-10));
    }
}
class OutOfException extends RuntimeException
{
    public OutOfException(String msg)
    {
        super(msg);
    }
}
3.finally代码块虽然在return 语句之前,但finally代码块不能通过重新给变量赋值的方式来改变return 语句的返回值
public class Test1
{
    public static int test()
    {
        int t = 0;
        try{
            return t;
        }finally{
            t = 10;
        }
    }
    public static void main(String[] args)
    {
        System.out.println(test());
    }
}
打印结果是: 0
4.建议不要在finally语句中使用return 语句,因为它会导致两种错误,第一种是覆盖try或者catch语句的return 的返回值,
public class Test1
{
    public static int test()
    {
        int t = 0;
        try{
            return t;
        }finally{
            return 10;
        }
    }
    public static void main(String[] args)
    {
        System.out.println(test());
    }
}
打印结果是: 10
import java.util.Scanner;
import java.util.InputMismatchException;
public class Test1
{
    public static int test()
    {
        Scanner in = new Scanner(System.in);
        try{
            int t = in.nextInt();
            return t;
        }catch(InputMismatchException e)
        {
            System.out.println("请输入整型数");
            return 20;
        }
        finally{
            return 10;
        }
    }
    public static void main(String[] args)
    {
        System.out.println(test());
    }
}
不管输入什么,返回的都是10
第二种是丢失异常,因为由于try语句中有return 语句所以会先执行finally 语句,但由于finally 的return,退出该方法了,因此会丢失异常
public class WithReturn
{
    public static int methodA(int money) throws OutOfException
    {
        if(money < 0)
        {
            throw new OutOfException("no money");
        }
        return money;
    }
    public static int methodB(int money)
    {
        System.out.println("begin");
        try{
            return methodA(money);
        }catch(OutOfException e)
        {
            throw new Exception("异常");
        }finally
        {
            return 100;
        }
    }
    public static void main(String[] args)
    {
        System.out.println(methodB(-10));
    }
}
class OutOfException extends RuntimeException
{
    public OutOfException(String msg)
    {
        super(msg);
    }
}
打印结果是:begin
           100
原创粉丝点击