throwsdemo.java

来自「《Java程序设计》第6、7、8章的源代码。有需要者请」· Java 代码 · 共 39 行

JAVA
39
字号
import java.io.*;	
public class ThrowsDemo{	
static double c;	
public static  void main(String []args){
BufferedReader readin=new BufferedReader(new InputStreamReader(System.in));
	   try{
	           System.out.println("请任意输入一个被除数(数字): ");	
	           String  input1=readin.readLine();       //会产生 IOException
	           float a=Float.parseFloat(input1);        //会产生 NumberFormatException
	           System.out.println("请任意输入一个非零的除数: ");
String  input2=readin.readLine();       //会产生 IOException	
	           float b=Float.parseFloat(input2);       //会产生 NumberFormatException
	           c=division(a,b);	
	   }catch(IOException ioe){
	           System.out.println("系统输出入有问题 ");
	           System.out.println(ioe.getMessage());
	           System.out.println("程序无法处理即将中断 ");
	           System.exit(0);
	   }catch(NumberFormatException nfe){
System.out.println("所输入的数值是 :");
	           System.out.println(nfe.getMessage());
	           System.out.println("程序无法处理即将中断 ");		
	           System.exit(0);
	   }catch(ArithmeticException ae){
	            System.out.println(ae.getMessage());
	            System.exit(0);
	   }finally{
	            System.out.println("两数相除的结果是 :"+'\n'+c);
	             System.exit(0);
	   }
}
static double division(double x, double y)throws ArithmeticException{
if(y==0) throw new ArithmeticException("除数不能为0,否则结果是无限大 ");
	           double result;
	   result=x/y;
	   return result;
}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?