設計一個 Java 程序,自定義異常類,從命令行(鍵盤)輸入一個字符串,如果該字符串值為“XYZ”,則拋出一個異常信息“This is a XYZ”,如果從命令行輸入 ABC,則沒有拋出異常。(只有 XYZ 和 ABC 兩種輸入)。
class xyz
{
public void test(String x)
{
if(x.equals("xyz"))
{
try{ throw new exception(x);}
catch(exception e){e.printStackTrace();}
}
else
System.out.println("suit");
}
public static void main(String args[])
{
new xyz().test("xyz");
new xyz().test("abc");
}
}
class exception extends Exception
{
public exception(String x)
{
System.out.println("this is a "+x);
}
}