標題:
例外處理 (五) - 自訂例外類別1
[打印本頁]
作者:
tonyh
時間:
2019-1-7 19:53
標題:
例外處理 (五) - 自訂例外類別1
本帖最後由 tonyh 於 2020-8-20 15:09 編輯
除了捕捉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); //呼叫父類別的建構子
}
}
複製代碼
作者:
李沛昂
時間:
2019-1-7 20:32
import java.util.Scanner;
public class Pig
{
static Scanner s=new Scanner(System.in);
public static void main(String[] pigs) 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);
}
}
複製代碼
作者:
黃宇綸
時間:
2019-1-7 20:32
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);
}
}
複製代碼
作者:
洪翊庭
時間:
2019-1-7 20:34
import java.util.Scanner;
public class Ch20
{
static Scanner s=new Scanner(System.in);
public static void main(String[] args) throws MyException
{
float a,b;
System.out.print("輸入分子: ");
a=s.nextFloat();
System.out.print("輸入分母: ");
b=s.nextFloat();
if(b==0)
throw new MyException("嘿嘿嘿~分母不可為零喔!");
System.out.println(a+"/"+b+"="+a/b);
}
}
class MyException extends Exception
{
MyException(String str)
{
super(str);
}
}
複製代碼
作者:
黃宇瑄
時間:
2019-1-7 20:34
import java.util.Scanner;
public class Ch51
{
static Scanner s=new Scanner(System.in);
public static void main(String[] pig) 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);
}
}
複製代碼
作者:
曾堂桂
時間:
2019-1-7 20:37
import java.util.Scanner;
public class Ch55
{
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);
}
}
複製代碼
作者:
洪翊展
時間:
2019-1-7 20:39
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);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2