关于异常的问题

abelking 2007-11-07 03:06:33
public class test3
{
public void method1(int x) throws Exception
{
try
{
method2(x);
}
catch (NegativeArraySizeException e)
{
System.out.println("Checkpoint 1");
}
finally
{
System.out.println("Checkpoint 2");
}
System.out.println("Checkpoint 3");
}

public void method2(int x) throws Exception
{
if (x < 0)
{
throw new NegativeArraySizeException();
}
}

static public void main(String[] args) throws Exception
{
test3 t = new test3();
t.method1(-55);
System.out.println("Checkpoint 4");

}

}
结果是
Checkpoint 1
Checkpoint 2
Checkpoint 3
Checkpoint 4
====================================
1 我记得异常捕捉之后,程序是会退出的。那为什么Checkpoint 3和Checkpoint 4还会被打印?是我记错了还是另有讲究?
2 方法名之后的throws异常和方法中的throw异常有什么联系?是不是必须为父子类的关系?
3 整个异常处理过程我可不可以这样理解:
method2中抛出new NegativeArraySizeException()异常对象后,因为method2没有进行处理,所以通过method2的throws Exception的方式把new NegativeArraySizeException()交给调用method2的方法(也就是method1)。在method1中处理异常。如果这是个method1也处理不了的异常,就再通过method1的throws Exception来抛回给main。
如果此理解不对,请指教。主要不明白方法和语句都在抛到底是什么意思。

谢谢!!!
...全文
185 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
abelking 2007-11-13
  • 打赏
  • 举报
回复
zai ding yi xia!
abelking 2007-11-12
  • 打赏
  • 举报
回复
ding yi xia!
abelking 2007-11-11
  • 打赏
  • 举报
回复
实际上method2抛出的new NegativeArraySizeException()在metood1中是可以处理的。如果我把代码这样修改为什么会报错?
public   class   test3 
{
public void method1(int x)
{
try
{
System.out.println(" see ");
method2(x); //提示出错:Unhandled exception type Exception
System.out.println(" no see ");
}
catch (NegativeArraySizeException e)
{
System.out.println( "Checkpoint 1 ");
}


finally
{
System.out.println( "Checkpoint 2 ");
}
System.out.println( "Checkpoint 3 ");
}

public void method2(int x) throws Exception
{
if (x < 0)
{
throw new NegativeArraySizeException();
}
}

static public void main(String[] args) throws Exception
{
test3 t = new test3();
for(int i =1;i<5;i++){
t.method1(-55);
System.out.println("for "+i);
}
System.out.println( "Checkpoint 4 ");

}

}

也就是把method1得throws Exception删去,method2的throws Exception仍然保留。结果出错。
所以我猜:是不是method2中throw出的异常要提交给method2的throws Exception ,让方法再包装一下然后再throws给调用它的方法?也就是在编译阶段,method1只知道method2可能会抛出Exception、而不知道实际上抛出的是NegativeArraySizeException,method1以为自己只能捕捉NegativeArraySizeException所以才在方法名后又将Exception抛出给main.这样的猜测对吗?
否则实际上meyhod1中可以的处理的NegativeArraySizeException为什么在删去method1的throws Exception后就不能处理了呢?

麻烦大家指教。谢谢!!!
lihaifeng0412 2007-11-09
  • 打赏
  • 举报
回复
说的都不错啊
hanfengthinker 2007-11-09
  • 打赏
  • 举报
回复
ding xia
胡矣 2007-11-09
  • 打赏
  • 举报
回复
throw 在程序中抛出异常 (若不是运行时异常) 则需要在类中throws异常(throw的异常应该为throws的子集)
楼主在method2中抛出了异常 如果在test3 中调用的时候 若try{}catch(Exception e){}捕获异常的话 则会按照catch中的代码来处理异常
并向下继续执行 若未做任何处理 只是在类中继续抛出异常且上层也没有在捕获该异常 则执行时候会打印出异常信息 并退出程序

代码可以这样修改

public class test3 {
public void method1(int x) {
try {
method2(x);

} catch (NegativeArraySizeException e) {
System.out.println("Checkpoint 1 ");
} finally {
System.out.println("Checkpoint 2 ");
}
System.out.println("Checkpoint 3 ");
}

public void method2(int x) {
if (x < 0) {
throw new NegativeArraySizeException();
}
}

static public void main(String[] args) {
test3 t = new test3();
t.method1(-55);
System.out.println("Checkpoint 4 ");

}

}
abelking 2007-11-08
  • 打赏
  • 举报
回复
ls大哥别只是帮忙缩进阿
「已注销」 2007-11-08
  • 打赏
  • 举报
回复
csdn不是增加了代码功能么?

public class test3 {
public void method1(int x) throws Exception {
try {
method2(x);
} catch (NegativeArraySizeException e) {
System.out.println("Checkpoint 1 ");
} finally {
System.out.println("Checkpoint 2 ");
}
System.out.println("Checkpoint 3 ");
}

public void method2(int x) throws Exception {
if (x < 0) {
throw new NegativeArraySizeException();
}
}

static public void main(String[] args) throws Exception {
test3 t = new test3();
t.method1(-55);
System.out.println("Checkpoint 4 ");

}

}
abelking 2007-11-08
  • 打赏
  • 举报
回复
似乎不能缩进
abelking 2007-11-08
  • 打赏
  • 举报
回复
打印出得风格的确太差 我重新贴出来
============================================
public class test3
{
public void method1(int x) throws Exception
{
try
{
method2(x);
}
catch (NegativeArraySizeException e)
{
System.out.println( "Checkpoint 1 ");
}
finally
{
System.out.println( "Checkpoint 2 ");
}
System.out.println( "Checkpoint 3 ");
}

public void method2(int x) throws Exception
{
if (x < 0)
{
throw new NegativeArraySizeException();
}
}

static public void main(String[] args) throws Exception
{
test3 t = new test3();
t.method1(-55);
System.out.println( "Checkpoint 4 ");

}

}
===========
语句中既然已经throw了,为什么方法中还要来个throws?
是不是语句中throw出的异常要提交给方法,让方法再包装一下然后再throws给调用它的方法?
beijihu3 2007-11-08
  • 打赏
  • 举报
回复
1 异常捕捉之后,程序是会退出的--退出的是相应的try语句块
2 用finally声明的语句块不管有没有产生异常都必须去执行的,所以有Checkpoint 2 和Checkpoint 3
3 你在main中写了System.out.println( "Checkpoint 4 ")所以有Checkpoint 4,异常只是退出相应的try语句块而已
chan08 2007-11-07
  • 打赏
  • 举报
回复
首先,你打印出的程序的编程风格太差了—_—!!
对于你的问题:
1.java异常机制就是发现异常-提出异常-假设程序无异常,程序继续进行。即:明显的告诉程序员异常在哪里,并且在假设没有发生异常的情况下运行出程序的执行结果。所以,在本程序完美的catch到异常之后,程序继续顺序运行。
2.throws的是位于函数声明处,提醒编程人员自己:这是个可以throw出异常的函数,要小心!在调用这样的函数的时候,通常要把此函数体置于try{}catch(Exception ex){}之中。
throw是位于函数体内部,作用就是试着运行代码,随时等着错误的抛出。
不知道我说的是不是你想要的:)
abelking 2007-11-07
  • 打赏
  • 举报
回复
ding ding
看看还有没有其他见解
blue_winding 2007-11-07
  • 打赏
  • 举报
回复


我的理解是你在method2方法中throw new NegativeArraySizeException();

在method1方法中捕获异常,执行catch语句块中的内容,但JAVA虚拟机(JVM)不会中断执行,

但是如果你没有在method1方法中捕获异常,不对异常进行处理,则JVM回中断执行
neptune0229 2007-11-07
  • 打赏
  • 举报
回复
是你记错了,出现异常只是中断try里的操作,并不会影响后面catch和finally的执行
除非你在catch的时候主动中断程序运行

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧