標題:
例外處理 (五) - 自訂例外類別1
[打印本頁]
作者:
tonyh
時間:
2021-11-20 20:56
標題:
例外處理 (五) - 自訂例外類別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); //呼叫父類別的建構子
}
}
複製代碼
作者:
陳志祐
時間:
2021-11-27 19:31
本帖最後由 陳志祐 於 2021-11-27 19:32 編輯
import java.util.Scanner;
public class Ch56
{
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);
}
}
複製代碼
作者:
林鼎傑
時間:
2021-11-27 19:35
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("D能兒,分母不可為零!");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
public MyException(String str)
{
super(str);
}
}
複製代碼
作者:
余柏緯
時間:
2021-11-27 19:38
import java.util.Scanner;
public class Ch03 {
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);
}
}
複製代碼
作者:
朱奕祐
時間:
2021-11-27 19:39
import java.util.Scanner;
public class Ch01{
static Scanner s=new Scanner(System.in);
publc static void main(String[] args) throws Exception{
float x,y;
System.out.print("輸入分子: ");
x=s.nextFloat();
System.out.print("輸入分母: ");
y=s.nextFloat();
if(y==0)
throw new Exception("分母不可為零");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
MyException(String str) {
super(str);
}
}
複製代碼
作者:
許洧熏
時間:
2021-11-27 19:40
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);
}
}
複製代碼
作者:
呂尚霖
時間:
2021-11-27 19: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);
}
}
複製代碼
作者:
黃子倢
時間:
2021-11-27 19: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);
}
}
複製代碼
作者:
蔡寓珉
時間:
2021-11-27 19:42
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));
}
}
複製代碼
作者:
王秉鈞
時間:
2021-11-27 19:43
import java.untl.Scanner;
public class Ch24 {
public static void main(String[] args) throw MyException
java.util.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(String str)
{
super(str);
}
}
}
複製代碼
作者:
林佑宸
時間:
2021-11-27 19:43
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);
}
}
複製代碼
作者:
俞成章
時間:
2021-11-27 19:49
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);
}
}
複製代碼
作者:
林土水
時間:
2021-11-30 20:46
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); //呼叫父類別的建構子
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2