標題:
例外處理 (五) - 自訂例外類別1
[打印本頁]
作者:
tonyh
時間:
2020-10-16 19:33
標題:
例外處理 (五) - 自訂例外類別1
除了捕捉Java拋出的例外物件,還可以利用關鍵字throw自行拋出例外物件。若在方法中拋出例外物件後,沒以try catch語法立即處理,則需在方法宣告列後方以throws關鍵字聲明該方法將會拋出例外物件,以強迫呼叫者處理例外。
import java.util.Scanner;
public class Ch51
{
static Scanner s=new Scanner(System.in);
//方法中發生例外但沒處理,以throws語法拋給呼叫者處理
public static void main(String[] args) throws MyException
{
float x,y;
System.out.print("輸入分子: ");
x=s.nextFloat();
System.out.print("輸入分母: ");
y=s.nextFloat();
if(y==0)
throw new MyException("嘿嘿嘿~分母不可為零喔!");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str); //呼叫父類別的建構子
}
}
複製代碼
作者:
林政瑜
時間:
2020-10-16 20:37
import java.util.Scanner;
public class Ch02
{
static Scanner s=new Scanner(System.in);
public static void main(String[] args) throws MyException
{
float x,y;
System.out.print("輸入分子");
x=s.nextFloat();
System.out.print("輸入分母");
y=s.nextFloat();
if(y==0)
throw new MyException("分母不可為零");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str);
}
}
複製代碼
作者:
李宇澤
時間:
2020-10-16 20:41
import java.util.InputMismatchException;
import java.util.Scanner;
public class Ch01
{
public static void main(String args[])
{
int a,b,c=0;
while(true)
{
if(c==3)
{
System.out.println("錯誤嘗試過多! 程式跳出!");
return;
}
try
{
Scanner s=new Scanner(System.in);
System.out.print("請輸入分子: ");
a=s.nextInt();
System.out.print("請輸入分母: ");
b=s.nextInt();
System.out.println(a+"/"+b+"="+(a/b));
return;
}catch(ArithmeticException e)
{
c++;
System.out.println("運算錯誤! 分母不可為零!\n");
}catch(InputMismatchException e)
{
c++;
System.out.println("格式錯誤! 輸入須為整數!\n");
}
}
}
}
複製代碼
作者:
董宸佑
時間:
2020-10-16 20:41
import java.util.Scanner;
public class Ch51
{
static Scanner s=new Scanner(System.in);
//方法中發生例外但沒處理,以throws語法拋給呼叫者處理
public static void main(String[] args) throws MyException
{
float x,y;
System.out.print("輸入分子: ");
x=s.nextFloat();
System.out.print("輸入分母: ");
y=s.nextFloat();
if(y==0)
throw new MyException("嘿嘿嘿~分母不可為零喔!");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str); //呼叫父類別的建構子
}
}
複製代碼
作者:
黃辰昊
時間:
2020-10-16 20:41
import java.util.Scanner;
public class Ch01
{
public static void main(String args[]) throws MyException
{
Scanner s=new Scanner(System.in);
float x,y;
System.out.print("輸入分子: ");
x=s.nextFloat();
System.out.print("輸入分母: ");
y=s.nextFloat();
if(y==0)
throw new MyException("嘿嘿嘿~分母不可為零喔!");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str);
}
}
複製代碼
作者:
夏子涵
時間:
2020-10-16 20:42
import java.util.Scanner;
public class Ch01
{
static Scanner s=new Scanner(System.in);
public static void main(String[] args) throws MyException
{
float x,y;
System.out.print("輸入分子: ");
x=s.nextFloat();
System.out.print("輸入分母: ");
y=s.nextFloat();
if(y==0)
throw new MyException("分母不可為零喔!!!");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str);
}
}
複製代碼
作者:
劉愷鈞
時間:
2020-10-16 20:43
import java.util.InputMismatchException;
import java.util.Scanner;
public class Ch01
{
static Scanner s=new Scanner(System.in);
public static void main(String[] args)throws MyException
{
float x,y;
System.out.print("請輸入分子:");
x=s.nextFloat();
System.out.print("請輸入分母:");
y=s.nextFloat();
if(y==0)
throw new MyException("Hey分母不可為零!");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str);
}
}
複製代碼
作者:
沈子晏
時間:
2020-10-16 20:45
import java.util.Scanner;
public class Ch50 {
static Scanner s=new Scanner(System.in);
public static void main(String[] args) throws MyException
{
float x,y;
System.out.print("輸入分子: ");
x=s.nextFloat();
System.out.print("輸入分母: ");
y=s.nextFloat();
if(y==0)
throw new MyException("分母不能為0喔");
System.out.println(x+"/"+y+"="+(x/y));
System.out.println();
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str);
}
}
複製代碼
作者:
黃宥華
時間:
2020-10-16 20:46
import java.util.*;
public class Ch99
{
public static void main(String args[]) throws MyException
{
float x,y;
Scanner s=new Scanner(System.in);
System.out.print("請輸入分子: ");
x=s.nextFloat();
System.out.print("請輸入分母: ");
y=s.nextFloat();
if(y==0)
throw new MyException("分母不可為0");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str);
}
}
複製代碼
作者:
黃柏叡
時間:
2020-10-16 20:46
import java.util.Scanner;
public class Ch01
{
static Scanner s=new Scanner(System.in);
public static void main(String[] args) throws MyException
{
float x,y;
System.out.print("輸入分子: ");
x=s.nextFloat();
System.out.print("輸入分母: ");
y=s.nextFloat();
if(y==0)
throw new MyException("嘿嘿嘿~分母不可為零喔!");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyExcetion extends Exception
{
MyExcetion(String str)
{
super(str);
}
}
複製代碼
作者:
蔡忻霓
時間:
2020-10-16 20:47
import java.util.Scanner;
public class Ch51
{
static Scanner s=new Scanner(System.in);
public static void main(String[] args) throws MyException
{
float x,y;
System.out.print("輸入分子: ");
x=s.nextFloat();
System.out.print("輸入分母: ");
y=s.nextFloat();
if(y==0)
throw new MyException("嘿嘿嘿~分母不可為零喔!");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str);
}
}
複製代碼
作者:
陳宥穎
時間:
2020-10-16 20:49
import java.util.Scanner;
public class Ch30 {
static Scanner s=new Scanner(System.in);
public static void main(String[] args) throws MyException{
float x,y;
System.out.print("輸入分子: ");
x=s.nextFloat();
System.out.print("輸入分母: ");
y=s.nextFloat();
if(y==0)
throw new MyException("error");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str); //呼叫父類別的建構子
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2