arithmeticexception.java
来自「有关java编程的课件及附上有关的源代码」· Java 代码 · 共 69 行
JAVA
69 行
/*
* (c) 2005 Aptech Limited
* 版权所有
*/
/**
* 这个类生成一个算术异常。
*
* @version 1.0 2005 年 5 月 22 日
* @author Michael
*/
package SG5.Example1;
class ExceptionRaised {
/** 构造函数。 */
protected ExceptionRaised() {
}
/**
* 这个方法生成一个异常。
* @param operand1 是除法中的分子
* @param operand2 是除法中的分母
* @return 它将返回除法的余数。
*/
static int calculate(final int operand1, final int operand2) {
int result = operand1 / operand2; // 用户自定义方法
return result;
}
}
/**
* 这是 main 类。
*
*/
public class ArithmeticException {
/** 构造函数。 */
protected ArithmeticException() {
}
/**
* 唯一的条目指向类和应用程序的唯一进入点。
* @param args 字符串参数的数组。
*/
public static void main(String[] args) {
ExceptionRaised obj = new ExceptionRaised();
try {
/* 定义变量 result 以存储结果。 */
int result = obj.calculate(9, 0);
System.out.println(result);
} catch (Exception e) { // 异常对象
System.err.println("发生异常:" + e.toString());
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?