​catch块中的System.out.println(e.getMessage());这句是什么意思?

来源:1-5 Java 中的异常抛出以及自定义异常

wshyzx

2016-07-17 23:26

catch块中的System.out.println(e.getMessage());这句是什么意思?

自定义异常 以及使用



public class ExceptionTest extends Exception{

    public ExceptionTest(){

         

    }

    public ExceptionTest(String message){

        super(message);

    }

    public void dunkOrNot(int select) throws ExceptionTest{

        if(select==1){

            throw new ExceptionTest("你喝大了");

        }

        else{

            System.out.println("你没喝醉");

        }

    }

    public static void main(String[] args)throws ExceptionTest {

        ExceptionTest et=new ExceptionTest();

        int select=1;

        try{

        et.dunkOrNot(select);

        }

        catch(ExceptionTest e){

            System.out.println("发现异常:"+e.getMessage());

        }

    }

}

    结果是什么?


写回答 关注

3回答

  • wshyzx
    2016-07-18 00:38:34

    自定义异常 以及使用


    public class ExceptionTest extends Exception{

        public ExceptionTest(){

             

        }

        public ExceptionTest(String message){

            super(message);

        }

        public void dunkOrNot(int select) throws ExceptionTest{

            if(select==1){

                throw new ExceptionTest("你喝大了");

            }

            else{

                System.out.println("你没喝醉");

            }

        }

        public static void main(String[] args)throws ExceptionTest {

            ExceptionTest et=new ExceptionTest();

            int select=1;

            try{

            et.dunkOrNot(select);

            }

            catch(ExceptionTest e){

                System.out.println("发现异常:"+e.getMessage());

            }

        }

    }

        结果是什么?


  • Feater
    2016-07-18 00:33:08

    e.getMessage(); 只会获得具体的异常名称. 比如说NullPoint 空指针,就告诉你说是空指针...

  • 慕粉3562401
    2016-07-18 00:29:19

    输出捕捉到的异常信息,也就是那段描述性信息

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409784 学习 · 4339 问题

查看课程

相似问题