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
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
解决方案 »
- 在eclipsez中部署struts2后的后遗症。
- HQL查询语句报错 unexpected token
- 一段代码在eclipse3.3.2可以编译,但在eclipse3.4.2不行
- weblogic 的奇怪错误
- 编译deploy.wsdd时的问题
- SOS 一个简单的问题困扰我一天,No action instance for path /tes could be created
- 在Struts框架中的“Cannot retrieve mapping for action ”问题
- 请教:我们的系统是否需要采用EJB方式
- 怎么启动j2ee自代的cloudscape数据库的可视化管理工具?
- 问个很简单的问题,如何引用自己写的类,希望会的详细回答。
- 关于三大框架
- struts2 前台时间类型参数传递后台时丢失分秒时
自定义异常时:如果该异常的发生,无法在继续进行运算,
就让自定义异常继承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");
}
}