public class Captor { // 创建类
static int quotient(int x, int y) throws MyException { // 定义方法抛出异常
if (y < 0) { // 判断参数是否小于0
throw new MyException("除数不能是负数"); // 异常信息
}
return x / y; // 返回值
}

public static void main(String args[]) { // 主方法
try { // try语句包含可能发生异常的语句
int result = quotient(3, -1); // 调用方法quotient()
} catch (MyException e) { // 处理自定义异常
System.out.println(e.getMessage()); // 输出异常信息
} catch (ArithmeticException e) { // 处理ArithmeticException异常
System.out.println("除数不能为0"); // 输出提示信息
} catch (Exception e) { // 处理其他异常
System.out.println("程序发生了其他的异常"); // 输出提示信息
}
}
}return x / y; // 返回值  他返回到哪里去了 为什么要用它   int result = quotient(3, -1); // 调用方法quotient()      result有什么用  干嘛的 是不是吧数字赋值给result后  然后result在传给xy

解决方案 »

  1.   

    额。。这个。。return 返回到了调用这个方法的东西上,就你这个程序返回给了result
      

  2.   

    你既然定义了quotient()方法是int类型的返回值,return x/y这句话就表示调用此法后会有一个返回值,本例中将返回值给result
      

  3.   

    这个return就是返回,你给我什么  我就给你一个第一个整数除以第二个整数的结果而result这个变量记录的是quotient()方法返回的一个具体的 整型的  一个运算后的结果,怎么运算它不知道,只管记录住这个运算的结果而已
    自定义异常时:如果该异常的发生,无法在继续进行运算,
    就让自定义异常继承RuntimeException。
    对于异常分两种:
    1,编译时被检测的异常。

    2,编译时不被检测的异常(运行时异常。RuntimeException以及其子类)*/class FuShuException extends RuntimeException
    {
    FuShuException(String msg)
    {
    super(msg);
    }
    }
    class Demo
    {
    int div(int a,int b)throws Exception//throws ArithmeticException
    {

    if(b<0)
    throw new Exception("出现了除数为负数了");
    if(b==0)
    throw new ArithmeticException("被零除啦");
    return a/b;
    }
    }class ExceptionDemo 
    {
    public static void main(String[] args) 
    {

    Demo d = new Demo();

    int x = d.div(4,-9);
    System.out.println("x="+x);

    System.out.println("over");
    }
    }