ExceptionDemo1

package

practice2;

 

import

java.util.Scanner;

 

public

class ExceptionDemo2 {

 

public static void main(String[] args) {

 

try {

className();

} catch (Exception e) {

 

e.getMessage(); // it is wrong

System.err.println("class num out of bound");

e.printStackTrace();

} finally {

System.out.println("Welcome for advice");

}

 

}

 

private static void className() throws Exception {

Scanner input = new Scanner(System.in);

System.out.print("Please input the class Num(1~3): ");

 

int s = input.nextInt();

if (s == 1 || s == 2 || s == 3) {

switch (s) {

 

case 1:

System.out.println("C#");

break;

case 2:

System.out.println("Java");

break;

case 3:

System.out.println("HuiBian");

break;

}

} else {

throw new Exception("it is wrong");

}

}

}

原文地址:https://www.cnblogs.com/mabel/p/5945948.html