51,396
社区成员




public class Demo extends Exception {//自定义异常,必须继承Exception或其子类
public Demo() {
super();
}
public Demo(String code) {
super(code);
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("code:");
Scanner input = new Scanner(System.in);
try {
int code = input.nextInt();
show(code);
} catch (Demo e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("程序结束");
}
public static void show(int code) throws Demo{
if(code<1||code>3) {
throw new Demo("课程编码越界");//使用throws关键字声明异常对象
}else if (code==1) {
System.out.println("c++");
}else if(code==2){
System.out.println("java");
}
else {
System.out.println("mysql");
}
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("code:");
try {
while(true) {
show();
}
} catch (Demo e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("程序结束");
}
public static void show() throws Demo{
Scanner input = new Scanner(System.in);
int code = input.nextInt();
if(code<1||code>3) {
throw new Demo("课程编码越界");//使用throws关键字声明异常对象
}else if (code==1) {
System.out.println("c++");
}else if(code==2){
System.out.println("java");
}
else {
System.out.println("mysql");
}
}
}