標題:
例外處理 (五) - 自訂例外類別1
[打印本頁]
作者:
陳弘修
時間:
2021-1-16 10:15
標題:
例外處理 (五) - 自訂例外類別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-1-16 11:10
import java.util.Scanner;
public class OwO
{
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-1-16 11:15
import java.util.Scanner;
public class a01
{
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-1-16 11:16
本帖最後由 林羿丞 於 2021-1-16 11:23 編輯
package aaa;
import java.util.*;
public class aaa
{
public static void main(String[] args) throws MyException
{
Scanner s=new Scanner(System.in);
int x,y;
System.out.print("輸入分子: ");
x=s.nextInt();
System.out.print("輸入分母: ");
y=s.nextInt();
if(y==0)
throw new MyException("嘿嘿嘿~分母不可為零喔!");
System.out.println(x+"/"+y+"="+(x/y));
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str); //呼叫父類別的建構子
}
}
複製代碼
作者:
張淯祺
時間:
2021-1-16 11:16
import java.util.Scanner;
public class a01
{
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-1-16 11:17
package o1;
import java.util.Scanner;
public class a2
{
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-1-16 11:19
package a01;
import java.util.Scanner;
public class a01
{
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-1-23 09:54
package a01;
import java.util.*;
public class a01 {
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));
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str);
}
}
複製代碼
作者:
黃柏智
時間:
2021-1-23 10:13
package A01;
import java.util.Scanner;
public class A01 {
static Scanner s=new Scanner(System.in);
public static void main(String[] args)throws MyException {
float x,y;
System.out.println("輸入分子");
x=s.nextFloat();
System.out.println("輸入分母");
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);
}
}
複製代碼
回復
1#
陳弘修
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2