public void test() throws Exception{
try{
...
}catch(Exception e){
throw e;
}finally{
...
}
}如下形式调用test()方法:
try{
test();
}catch(Exception e){
System.out.println(e.getMessage());
}
输出结果为空,如果将test方法中的finally块去掉就能输出错误信息,这是怎么回事啊?
try{
...
}catch(Exception e){
throw e;
}finally{
...
}
}如下形式调用test()方法:
try{
test();
}catch(Exception e){
System.out.println(e.getMessage());
}
输出结果为空,如果将test方法中的finally块去掉就能输出错误信息,这是怎么回事啊?
解决方案 »
- JTree with CheckBoxes
- java的变量可以使用时再定义吗?
- 如何将oracle中 zip流写入zip文件
- 怎样复制一个文件?
- 为什么那个关联字段就是添加不进去了,弄了好久了~~~
- ●●● 200本Java书籍下载...........................................
- 招聘
- 一道题目
- 一个自定义java2 Swing JTree的问题??很有挑战性!!
- 数据迁移问题,求大家给建议
- 求救?Exception in thread "main" java.lang.UnsupportedClassVersionError:
- addActionListener(this)里的this指哪个对象引用?
public static int test(){
try{
return 1;
}
catch(Exception e){
}
finally{
return 2;
}
}这个更能说明问题.
内层(test())try产生异常.内层catch已经捕获,外层
catch(Exception e){System.out.println(e.getMessage());}
就不会再捕获执行了.
去掉finally也不会捕获执行.
try{
...
throw new Exception("See me?"); }catch(Exception e){
throw e;
}finally{
...
}
}
test();
}catch(Exception e){
System.out.println(e.getMessage());
}
这里执行到test()时执行下面的test函数
public void test() throws Exception{
try{
...
}catch(Exception e){
throw e;
}finally{
...
}
}
而这个test函数在执行完finally里后程序就结束了,它不会返回到下面程序中的catch再执行,所以不会有输出,当你去掉了finally后它就会再执行完上面函数的方法后继续执行下面程序中的catch语句,就会返回e.getMseesage()的值了
try{
test();
}catch(Exception e){
System.out.println(e.getMessage());
}
貌似有2个 try 就要 catch 2次...去不去掉 finally 都没什么问题.public class Test
{
void test() throws Exception
{
try
{
int a = 1/0;
}catch(Exception e){
throw e;
}
finally
{
System.out.println("finally");
}
}
public static void main(String[] args)
{
try
{
new Test().test();
}
catch (Exception e)
{
System.out.println("Do you see me ?");
}
}
}
/*
output:
finally
Do you see me ?
*/public class Test
{
void test() throws Exception
{
try
{
int a = 1/0;
}catch(Exception e){
throw e;
}
/*finally
{
System.out.println("finally");
}*/
}
public static void main(String[] args)
{
try
{
new Test().test();
}
catch (Exception e)
{
System.out.println("Do you see me ?");
}
}
}
//output:Do you see me ?
如果没有把异常抛出,异常被test()的try吞了情况就不一样了.
public class Test
{
void test() throws Exception
{
try
{
int a = 1/0;
}catch(Exception e){
//throw e;
}
/*finally
{
System.out.println("finally");
}*/
}
public static void main(String[] args)
{
try
{
new Test().test();
}
catch (Exception e)
{
System.out.println("Do you see me ?");
}
}
}
//output:(什么也没有...)