黑马程序员技术交流社区

标题: 异常 [打印本页]

作者: zouxx    时间: 2015-4-6 20:45
标题: 异常
class Demo
{
        public static void main(String[] args)
        {
                try
                {
                        showExce();
                        System.out.println("A");
                }
                catch(Exception e)
                {
                        System.out.println("B");
                }
                finally
                {
                        System.out.println("C");
                }
                System.out.println("D");
        }
        public static void showExce()throws Exception
        {
                throw new Exception();
        }
}
为什么下面这个程序打印"A"语句读不到,编译失败了而上面的却可以
class Demo
{       
        public static void func()
        {
                try
                {
                        throw  new Exception();
                        System.out.println("A");
                }
                catch(Exception e)
                {
                        System.out.println("B");
                }
        }
        public static void main(String[] args)
        {
                try
                {
                        func();
                }
                catch(Exception e)
                {
                        System.out.println("C");
                }
                System.out.println("D");
        }
}
作者: Searching    时间: 2015-4-6 20:55
showExce只是告诉程序运行时可能出现异常,而你直接new了一个exception就不同了,抛出异常后后面的代码就不会被执行了。
作者: 大神在何方    时间: 2015-4-6 23:20
因为这样都已经报错了。~你在打印之前已经抛出了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2