Topic: 怎么理解这句话呢?

  Print this page

1.怎么理解这句话呢? Copy to clipboard
Posted by: zeeho_81
Posted on: 2004-07-01 13:56

考虑这样一种情况,在该情况中main()方法调用另一个方法(比如,first()),然后它调用另一个(比如,second())。如果在second()中发生异常,那么必须做一个检查来看看该异常是否有一个catch;如果没有,那么对调用栈(first())中的下一个方法进行检查,然后检查下一个(main())。如果这个异常在该调用栈上没有被最后一个方法处理,那么就会发生一个运行时错误,程序终止执行
哪位大大写个程序段,解释一下啊

2.Re:怎么理解这句话呢? [Re: zeeho_81] Copy to clipboard
Posted by: heaven
Posted on: 2004-07-01 14:21

如果在second()中发生异常,那么必须做一个检查来看看该异常是否有一个catch:
public class Test {
public static void main(String[] strs) throws Exception {
first();
}
private static void first() throws Exception {
second();
}

private static void second(){
try {
throw new Exception("Your Exception");
} catch (Exception e) {
System.out.println("something wrong in second method.");
}
}
}

V:/j2sdk1.4.2_03\bin\java  -classpath . Test
something wrong in second method.

如果没有,那么对调用栈(first())中的下一个方法进行检查
public class Test {
public static void main(String[] strs) throws Exception {
first();
}

private static void first(){
try {
second();
} catch (Exception e) {
System.out.println("something wrong in first method.");
}
}

private static void second() throws Exception {
throw new Exception("Your Exception");
}
}

V:/j2sdk1.4.2_03\bin\java  -classpath . Test
something wrong in first method.

如果还是没有,就检查下一个(main())
public class Test {
public static void main(String[] strs) {
try {
first();
} catch (Exception e) {
System.out.println("something wrong in main method.");
}
}

private static void first() throws Exception {
second();
}

private static void second() throws Exception {
throw new Exception("Your Exception");
}
}

V:/j2sdk1.4.2_03\bin\java  -classpath . Test
something wrong in main method.

如果全部没有检查,发生运行时错误:
public class Test {
public static void main(String[] strs) throws Exception {
first();
}
private static void first() throws Exception {
second();
}

private static void second() throws Exception {
throw new Exception("Your Exception");
}
}

V:\j2sdk1.4.2_03\bin\java -classpath .  Test
java.lang.Exception: Your Exception
at Test.second(Test.java:13)
at Test.first(Test.java:9)
at Test.main(Test.java:5)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.intellij.rt.execution.application.AppMain.main(Unknown Source)
Exception in thread "main" Process terminated with exit code 1

ft,真是太麻烦了,以后再也不干这种事了。

3.Re:怎么理解这句话呢? [Re: zeeho_81] Copy to clipboard
Posted by: zeeho_81
Posted on: 2004-07-01 14:37

great thanks ar

4.Re:怎么理解这句话呢? [Re: zeeho_81] Copy to clipboard
Posted by: 心处理器
Posted on: 2004-07-01 14:43

那本烂书写的定义,烂的要死

看完以下代码包你会
-----------------------------------------------------------------------------------------------------

public class Test
{
public static void main(String [] args){
first();
}
public static void first() throws Exception{ //抛出异常给引用者(main())处理
second();
}
public static void second() throws Exception{ //抛出异常给引用者(first())处理
throw new Exception();
}
}

/**
*结果谁都没有处理,程序终止
*/

------------------------------------------------------------------------------------------------------
以下是运行正常的代码:
------------------------------------------------------------------------------------------------------

public class Test
{
public static void main(String [] args){
first();
}
public static void first(){ //本方法包含try-catch块,处理了所引用的second()抛出的错误。
try{
second();
}
catch(Exception e){
System.out.println("a");
}
}
public static void second() throws Exception{ //抛出异常给引用者(first())处理
throw new Exception();
}
}

/**
*运行正常,打印出 a
*/

5.Re:怎么理解这句话呢? [Re: zeeho_81] Copy to clipboard
Posted by: javaguan2
Posted on: 2004-08-23 11:05

关于抛异常的问题,java本身有一个缺陷,(好象被thinking in java指出过)
就是throw两个异常的时候,第一个异常丢了,无从找寻

6.Re:怎么理解这句话呢? [Re: zeeho_81] Copy to clipboard
Posted by: jigsaw
Posted on: 2004-08-23 20:14

请问。。怎么抛出两个异常?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923