package com.great.day02_01;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionUsage {
public static void main(String[] args) {
/* System.out.println("请输入一个整数:");
//int a = 1/0;
try{
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
}catch(ArithmeticException ex){
System.out.println("程序出错了...");
ex.printStackTrace();
}catch( InputMismatchException ex){
System.out.println("程序出错了...111");
ex.printStackTrace(); //打印堆栈(异常)信息
}catch(Exception ex){
System.out.println("程序出错了...222");
ex.printStackTrace();
}finally{
System.out.println("finally");
}
System.out.println("请输入一个整数:");
try{
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
}catch(ArithmeticException ex){
System.out.println("程序出错了...");
ex.printStackTrace();
}finally{
System.out.println("finally");
}
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");*/
dividebyZero();
}
public static void dividebyZero()
{
try{
System.out.println("请输入数字");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
int temp = 13/i;
System.out.println("所得值是:"+temp);
}catch(InputMismatchException ex){
System.out.println("输入错误...");
}catch(ArithmeticException ex){
System.out.println("分母不能为0...");
}finally{
System.out.println("finally...");
}
}
}
输出结果:
请输入数字
a
输入错误…
finally…