?? throwsdemo.java
字號:
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 + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -