?? throwsdemo.java
字號(hào):
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("請(qǐng)任意輸入一個(gè)被除數(shù)(數(shù)字): ");
String input1=readin.readLine(); //會(huì)產(chǎn)生 IOException
float a=Float.parseFloat(input1); //會(huì)產(chǎn)生 NumberFormatException
System.out.println("請(qǐng)任意輸入一個(gè)非零的除數(shù): ");
String input2=readin.readLine(); //會(huì)產(chǎn)生 IOException
float b=Float.parseFloat(input2); //會(huì)產(chǎn)生 NumberFormatException
c=division(a,b);
}catch(IOException ioe){
System.out.println("系統(tǒng)輸出入有問題 ");
System.out.println(ioe.getMessage());
System.out.println("程序無法處理即將中斷 ");
System.exit(0);
}catch(NumberFormatException nfe){
System.out.println("所輸入的數(shù)值是 :");
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("兩數(shù)相除的結(jié)果是 :"+'\n'+c);
System.exit(0);
}
}
static double division(double x, double y)throws ArithmeticException{
if(y==0) throw new ArithmeticException("除數(shù)不能為0,否則結(jié)果是無限大 ");
double result;
result=x/y;
return result;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -