黑马程序员技术交流社区

标题: 求助有关异常的 [打印本页]

作者: 陈祥厅    时间: 2013-1-7 15:07
标题: 求助有关异常的
本帖最后由 陈祥厅 于 2013-1-8 23:35 编辑

class Demo
{
        public static void func()
        {
                try
                {
                        throw new Exception();
                        System.out.println("A");
                }
                catch(Exception e)
                {
                        System.out.println("B");
                }
        }
        pubic static void main(String[] args)
        {
                try
                {
                        func();
                }
                catch(Exception e)
                {
                        System.out.println("C");
                }
                System.out.println("D");

        }
}
//编译失败,因为打印“A”的输出语句执行不到。----------------------------打印A的执行不到但是上面不是抛异常了么为啥下面catch里面的不执行啊
作者: 金鑫    时间: 2013-1-7 15:12
  try
                {
                        throw new Exception();
                        System.out.println("A");
                }
因为你在try语句中抛出了throw new Exception();下面的catch语句捕获的是你抛出的Exception(),A自然就执行不到了
作者: 杨建聪    时间: 2013-1-7 15:17
A是肯定执行不到的,在try中throw new Exception();下面不可以放任何语句,都是执行不到的
作者: 陈祥厅    时间: 2013-1-7 15:18
金鑫 发表于 2013-1-7 15:12
try
                {
                        throw new Exception();

catch里面的打印B的语句怎么没执行呢
作者: 杨建聪    时间: 2013-1-7 15:28
  try
                {
                        func();
                }
你把func()方法放在try里面了,当主函数执行到try,只会抛出异常,catch捕捉到的才可以打印
作者: 金鑫    时间: 2013-1-7 15:29
陈祥厅 发表于 2013-1-7 15:18
catch里面的打印B的语句怎么没执行呢

你把func()方法里的try中的A语句去掉,程序正常运行,结果是B,D

作者: 范德农    时间: 2013-1-7 15:54
//逐条分析吧
class Demo
{
        public static void func()
        {
                try
                {
                       //你在这里抛出了一个新异常,那么执行过程时一发现异常就会直接跳转到catch,根本就不会再执行tr中y抛出异常语句后面的内容了                        throw new Exception();
                        System.out.println("A");
                }
                catch(Exception e)
                {
                        System.out.println("B");
                }
        }
        public static void main(String[] args)
        {
              /*因为上面的func()语句的异常在内部已经解决。所以对于此事try()中的func()实际上就只包含System.out.println("A");
                同时因为throw new Exception()发生在System.out.println("A");之前,故不执行这句,会直接跳转catch(),于是这便产
           生冲突了,若将func()中的throw new Exception和System.out.println("A");顺序进行调换,则不会出现问题*/
                try
                {
                        func();
                }
                catch(Exception e)
                {
                        System.out.println("C");
                }
                System.out.println("D");

        }
}
总结:你可以去掉A,那么结果是B\D,也可以调换A和throw new Exception()的顺序,这样结果是A、B、D。但是C不会执行,因为根本没有EXception给它catch了。
作者: 范德农    时间: 2013-1-7 15:54
//逐条分析吧
class Demo
{
        public static void func()
        {
                try
                {
                       //你在这里抛出了一个新异常,那么执行过程时一发现异常就会直接跳转到catch,根本就不会再执行tr中y抛出异常语句后面的内容了                        throw new Exception();
                        System.out.println("A");
                }
                catch(Exception e)
                {
                        System.out.println("B");
                }
        }
        public static void main(String[] args)
        {
              /*因为上面的func()语句的异常在内部已经解决。所以对于此事try()中的func()实际上就只包含System.out.println("A");
                同时因为throw new Exception()发生在System.out.println("A");之前,故不执行这句,会直接跳转catch(),于是这便产
           生冲突了,若将func()中的throw new Exception和System.out.println("A");顺序进行调换,则不会出现问题*/
                try
                {
                        func();
                }
                catch(Exception e)
                {
                        System.out.println("C");
                }
                System.out.println("D");

        }
}
总结:你可以去掉A,那么结果是B\D,也可以调换A和throw new Exception()的顺序,这样结果是A、B、D。但是C不会执行,因为根本没有EXception给它catch了。
作者: 张森    时间: 2013-1-7 17:34
首先LZ 这个异常是属于编译时异常,不是属于运行时异常。

try{
    throw new Exception();//您这里已经跑出去了一个异常,那么他下面的代码肯定不会执行,虚拟机不会做这种无用的操作,所以你在编译的时候,他就会报下面的打印A不可到达
    System.out.println("A");
}
catch(Exception e)
{
System.out.println("B");//然后您这里对你上面的异常进行了捕捉,所以您下面在主函数中的try catch 是捕捉不到上面的抛出来的异常,因为他已经被捕捉了
}

try
{
func();
}
catch(Exception e) //您这里是不做不到您上面抛出的 throw new Exception的 因为他自己已经在fun()方面里面进行了捕捉
{
System.out.println("C");
}

作者: 何竹冬    时间: 2013-1-7 22:03
你好
try语句里抛出异常后就会交给catch语句处理所以下面的语句肯定执行不到
所以会报错。




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