选择题:AAADC
1.使用多重 catch 语句时,异常子类一定要位于异常父类之前。应把catch(Exception e)放在最后一个catch块。
2.`package Exception;
public class Person {
private String name="";
private int age=0;
private String sex=“男”;
public void setAge(int age) throws Exception{
if(age>0&&age<100)
this.sex=sex;
else{
throw new Exception(“年龄必须在1~100之间!”);
}
}
public void print(){
System.out.println(this.name+"("+this.sex+","+this.age+“岁)”);
}
}
package Exception;
public class TextException2 {
public static void main(String[] args) {
Person person=new Person();
try{
person.setAge(199);
person.print();
}catch(Exception e){
e.printStackTrace();
}
}
}
进入if语句—〉抛出异常——〉进入catch—〉输出2----〉进入finally—>输出3—〉执行剩余代码—〉输出4
3.`package bdqn.t206.java5;
import java.util.Scanner;
public class TextName {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("请输入课程代号(1~3之间的数字):");
int num=scanner.nextInt();
switch(num){
case 1:
System.out.println("C#编程");
break;
case 2:
System.out.println("Java编程");
break;
case 3:
System.out.println("SQL基础");
break;
default:
System.out.println("你输入的不是1~3之间的数字!");
break;
}
System.out.println("欢迎提出建议!");
}
}
4.`public class word2 {
@SuppressWarnings(“unused”)
private int age;
public void setAge(int age) throws Exception {
if (age < 1 || age > 100) {
throw new Exception("年龄必须在1到100之间!");
} else {
this.age = age;
System.out.println("赋值成功");
}
}
public int num(String flag) throws Exception {
try {
int a = Integer.parseInt(flag);
return a;
} catch (Exception e) {
throw new Exception("必须输入1到100之间的数字!");
}
}
}
import java.util.Scanner;
public class word2_1 {
public static void main(String[] args) throws Exception{
word2 word = new word2();
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print("请输入1-100的数字:");
try {
int a = word.num(input.next());
word.setAge(a);
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.package bdqn.t206.java5;
public class Markdowm {
public static void main(String[] args) {
int arr[]=new int[3];
try{
arr[4]=1;
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("对不起,你设置的数组越界了!");
}
}
}