package hanjia;
import java.util.Scanner;
//throws关键字
public class hanjia{
//定义一个方法,使用throws声明抛出异常
public static void wodefkthrows() throws NumberFormatException,ArithmeticException,Exception{//捕获异常顺序是从小到大
Scanner sc=new Scanner(System.in);
System.out.println("请输入第一个数:");
//从键盘获取一个字符串
String str=sc.next();
//将不是整数的数字的字符串转换为整数,会引发NumberFormatException
int n1=Integer.parseInt(str);
System.out.println("输入第二个数字:");
//从键盘上获取一个整数
int n2=sc.nextInt();
System.out.println("输入的两个数相除结果是:"+n1/n2);
}
public static void main(String args[]) {
try {
//调用带抛出的异常序列的方法,调用上面的方法。
wodefkthrows();
}catch(NumberFormatException e) {
e.printStackTrace();
}catch(ArithmeticException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}
个人理解及资料参考,若有不足或者需要扩展请指点。
若有雷同,纯属巧合。