看了下try-catch-finally块的知识:
碰到了些问题:public class FinallyTest
{
public static void main(String[] args)
{
//System.out.println("main--- " + new FinallyTest().print());
} public int print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
return i;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
return i;
}
}
}
上面的代码不会报错。
可是如果我的print()方法没有返回值,也就是说print()的代码写成: public void print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
}
}就会报错!!!
Thread.sleep(1)块就要捕捉InterruptedException这个异常,
这是为什么啊?

解决方案 »

  1.   

    不知道你是在外面调用print()方法
    还是就在这个类调用如果在这个类mian()直接调用print()方法
    就应该把print()声明为Static
      

  2.   

    还有 你Catch的异常也写错了
      

  3.   


    public class FinallyTest
    {
        public static void main(String[] args)
        {
            //System.out.println("main--- " + new FinallyTest().print());
        }    public int print()
        {
            int i = -1;
            try
            {
                Thread.sleep(1);
                i = 1 / 0;//除以0,会有叫什么……byZero的异常
                return i;
            }
            catch (IndexOutOfBoundsException e)//异常错误
            {
            }
            finally
            {
                System.out.println("finally--- " + i);
                return i;
            }
        }
    }
      

  4.   

    我故意把异常写成IndexOutOfBoundsException的,
    我理解的是:只要是RuntimeException的话,放在catch()中,在编译时都是可以的。
    这里IndexOutOfBoundsException和ArithmeticException的效果应该是一样的。
      

  5.   

    跟return有关。你把代码改成这样也不会报错的。public class FinallyTest
    {
        public static void main(String[] args)
        {
            //System.out.println("main--- " + new FinallyTest().print());
            new FinallyTest().print();
        }    public void print()
        {
            int i = -1;
            try
            {
                Thread.sleep(1);
                i = 1 / 0;   
                //return;这个可写可不写
            }
            catch (IndexOutOfBoundsException e)
            {
            }
            finally
            {
                System.out.println("finally--- " + i);
                return;
            }
        }
    }
    但是如果把return写在try-catch-finally块外面,还是需要你捕捉InterruptedException的。
      

  6.   

        public void print()
        {
            int i = -1;
            try
            {
                Thread.sleep(1);
                i = 1 / 0;
            }
            catch (IndexOutOfBoundsException e)
            {
            }
            finally
            {
                System.out.println("finally--- " + i);
                return;
            }
        }这样就不会报错了
      

  7.   

    你try里面是Thread
    所以catch 的就一定要是InterruptedExceptionIndexOutOfBoundsException和ArithmeticException是运行期异常,
      

  8.   

    现在代码是这样,没有返回值return,不知道为什么会报错。
    而有返回值又不会报错呢。 public void print()
    {
    int i = -1;
    try
    {
    Thread.sleep(1);
    //i = 1 / 0;
    //return i;
    }
    catch (IndexOutOfBoundsException e)
    {
    }
    finally
    {
    System.out.println("finally--- " + i);
    //return i;
    }
    }输得代码会报错。
    而如果最后一句的//return i;修改为return就不会报错了。
    不知道为什么
      

  9.   

    我的理解是这样的:有返回值,后面的代码就不会被执行,直接跳到finally的代码块里,就像for循环一样,有了返回值就不会再执行下去了.
      

  10.   

    public class FinallyTest
    {
        public static void main(String[] args)
        {
            //System.out.println("main--- " + new FinallyTest().print());
        }    public void print()
        {
            int i = -1;
            try
            {
                Thread.sleep(1);
                i = 1 / 0;     
            }
            catch (IndexOutOfBoundsException e)
            {
            }
            finally
            {
                System.out.println("finally--- " + i);
                return;//这里加个return
            }
        }
    }
      

  11.   

    你的finally块中的return i;去掉,就会出现和第二个没有返回值的print()一样的错误,
    其实你的sleep()的异常InterruptedException是必须的捕捉的异常
      

  12.   

    也就是说你的finally块加上return i;就不用捕捉InterruptedException,不加,就得捕捉InterruptedException,why?????
      

  13.   

    有点明白了,return写在finally里边就要以保证这个方法可以正常的return,所以不管出现任何异常程序也可以继续执行下去.
    但是 return如果是写在外边,try块里边出了异常,程序是没办法执行的了.
      

  14.   

    如果在finally块中有return、continue语句,则会屏蔽掉抛出的异常。讨论这个问题,首先要对try、catch、和finally的概念清楚才行,
    而在这个帖子里,对finally的理解又更加重要。
    finally不管出现异常与否,都必须去执行的代码
    因此在代码中,必须执行finally中的代码
    PS:如果在try或者catch中强行终止JVM,就不会运行finally中的代码了。然后再来看代码,
    在你说的不会报错的情况下,
    其实你把catch块注释掉也没问题的。
    解释一下这个屏蔽:
    在这种情况下,有finally块,并且在try块中出现了异常,
    那么,finally块里的语句,是在执行了catch语句之后、退出方法之前运行的。
    先把代码这样改一下:public void print() throws Exception {
    int i = -1;
    try {
    Thread.sleep(1);
    i = 1 / 0;
    } catch (Exception e) {
    System.out.println("catch step1.");
    throw e;
    } finally {
    System.out.println("finally--- " + i);
    return;
    }
    }以上的代码, 当程序运行到catch块中throw e时,其实并没有抛出异常,而是先把它放在栈中,
    因为如果此时抛出了,那么方法就退出了。
     在finally块中有return语句,所以方法正常退出。也没有抛出异常。
    PS:
    1.你可以打印一下,在进入catch快后,打印了catch step1. 但并没有finally--- -1输出。
    2.你可以运行一下,看是否这个被我改过的程序有异常抛出,肯定没有,而确实有异常产生了。
      

  15.   

    当程序运行到catch块中throw e时,其实并没有抛出异常,而是先把它放在栈中,
    它还要去执行finally块中的代码。
    因为如果此时抛出了,那么方法就退出了。但这时候finally块还未必执行。
    而在finally块中恰好有return语句,所以方法正常退出。也就没有抛出异常。 
      

  16.   

    你的代码存在两个异常,你就捕获了一个,你打类应该继承Thread类。
      

  17.   

    确实是你说的在finally当中有了return的问题
    不过为什么return会屏蔽掉异常呢
    你的表述好混乱啊