try-catch-finally
看了下try-catch-finally块的知识:
碰到了些问题:
上面的代码不会报错。
可是如果我的print()方法没有返回值,也就是说print()的代码写成:
就会报错!!!
Thread.sleep(1)块就要捕捉InterruptedException这个异常,
这是为什么啊?
碰到了些问题:
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这个异常,
这是为什么啊?
...全文
相关推荐
Java try catch finally异常处理组合详解
通过try catch、try catch finally、多个catch和try finally等不同的异常处理方式,可以灵活地处理各种异常,提高程序的健壮性和可靠性。
try、catch、finally、return 执行顺序.doc
下面我们来详细讲解 try、catch、finally、return 执行顺序的规则和特点。首先,我们需要了解 try、catch、finally 语句的基本用法。
C++异常处理 try,catch,throw,finally的用法
C++标准异常:在C++标准异常中,我们可以使用try、catch、throw、finally四个关键字来处理异常。
使用try-catch-finally处理异常
Java、C#等许多编程语言提供了异常处理框架,其中“try-catch-finally”是常用的一种结构。下面我们将详细探讨这个主题。1.
关于Java中的try-catch-finally语句和return
"Java编程语言中的try-catch-finally语句是异常处理的关键构造,用于确保在发生异常时,特定的清理代码能够被执行。本文主要探讨了return语句与try-catch-finally块


Java SE
