package com.niit.exception;class MyException extends Exception {
private int exceptnumber;

MyException(int a) {
exceptnumber = a;
}

public String toString() {
return "MyException[" + exceptnumber + "]";
}
}
public class ExceptionExample {

static void makeexcept(int a) throws MyException {
System.out.println("called makeexcept(" + a + ").");
if(a == 0) {
Throw new MyException(a);
}
System.out.println("exit without exception");
}
public static void main(String[] args) {
try {
makeexcept(5);
makeexcept(0);
} catch(MyException e) {
System.out.println("caught " + e);
}
}}
这段代码是书中的例子程序,可是在MyEclipse中却在  Throw new MyException(a);  这一行报错,按照MyEclipse修改为Object Throw =  new MyException(a);但是结果却不一样了,变成了called makeexcept(5).
exit without exception
called makeexcept(0).
exit without exception。
按照程序的意思结果应该是
called makeexcept(5).
exit without exception
called makeexcept(0).
caught MyException[0]请知道的解释一下,谢谢

解决方案 »

  1.   

    if(a == 0) {
       Throw new MyException(a);
    }
    改为
    if(a == 0) {
       throw new MyException(a);
    }
    throw中的t要小写,它是关键字
      

  2.   

    static void makeexcept(int a) throws MyException方法里面使用Throws关键字说明该方法有可能回抛出异常,但不是一定会抛出异常。而方法里面Throw new MyException(a);使用Throw关键字,说明该方法一定会抛出异常,需要再调用该方法的位置main方法里面捕获异常
      

  3.   

    恩,我调试了下,需要将Throw关键字改成throw,运行的结果就是像你说的那样。
    从main方法开始, makeexcept(5);会调用alled makeexcept(5),exit without exception.
    makeexcept(0),调用alled makeexcept(0),然后捕获到异常执行caught MyException[0]