高淇java300集异常机制作业

1.以下关于异常的代码的执行结果是(C )。(选择一项)

1
2
3
4
5
6
7
8
9
10
11
12
public class Test {
    public static void main(String args[]) {
        try {
            System.out.print("try");           
            return;
        catch(Exception e){
            System.out.print("catch");
        }finally {
            System.out.print("finally");
        }
    }
}

A.try catch finally

B.catch finally

C.try finally

D.try

异常机制处理的顺序通常是 try-catch-finally

2.在异常处理中,如释放资源、关闭文件等由(C )来完成。(选择一项)

Atry子句

B.catch子句

C.finally子句

D.throw子句

3.阅读如下Java代码,其中错误的行是(A C )。(选择二项)

1
2
3
4
5
6
7
8
9
10
public class Student {
    private String stuId;
    public void setStuId(String stuId) throw Exception { // 1
        if (stuId.length() != 4) { // 2
            throws new Exception("学号必须为4位!"); // 3
        else {
            this.stuId = stuId;     //4
        }
    }
}

A.1

B.2

C.3

D.全部正确

1处为throws

3处为throw

4.下面选项中属于运行时异常的是(C D )。(选择二项)

A.Exception和SexException

B.NullPointerException和InputMismatchException

C.ArithmeticException和ArrayIndexOutOfBoundsException

D.ClassNotFoundException和ClassCastException

ArithmeticException  试图除以0

ArrayIndexOutOfBoundsException数组索引越界异常的方式

NullPointerException 空指针异常

ClassNotFoundException 无法找到指定的类异常

ClassCastException 数据类型转换时,有可能发生类型转换异常

InputMismatchException  属于CheckException异常

5.阅读如下Java代码,在控制台输入"-1",执行结果是(B)。(选择一项)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Demo {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入数字:");
        try {
            int num = input.nextInt();
            if (num < 1 || num > 4) {
                throw new Exception("必须在1-4之间!");
            }
        catch (InputMismatchException e) {
            System.out.println("InputMismatchException");
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

A.输出:InputMismatchException

B.输出:必须在1-4之间!

C.什么也没输出

D.编译错误

输入不为int  为字符char 字符串string  或者Boolean型时抛出A 属于CheckException异常

二、简答题

1. Error和Exception的区别。

Error是程序无法处理的错误,表示运行应用程序中较严重问题。大多数错误与代码编写者执行的操作无关,而表示代码运行时 JVM(Java 虚拟机)出现的问题

Exception是异常,需要进行捕获(try{} catch(){} finally{])或者抛出处理(throws)

2. Checked异常和Runtime异常的区别。

Checked异常已检查异常,这类异常在编译时就必须做出处理,否则无法通过编译

Runtime异常是运行时异常 ,这类异常通常是由编程错误导致的,所以在编写程序时,并不要求必须使用异常处理机制来处理这类异常,经常需要通过增加“逻辑处理来避免这些异常”。

3. Java异常处理中,关键字try、catch、finally、throw、throws分别代表什么含义?

try:try语句指定了一段代码,该段代码就是异常捕获并处理的范围,一个try语句必须带有至少一个catch语句块或一个finally语句块 。


catch:每个try语句块可以伴随一个或多个catch语句,用于处理可能产生的不同类型的异常对象。如果异常类之间有继承关系,在顺序安排上需注意。越是顶层的类,越放在下面,再不然就直接把多余的catch省略掉。 也就是先捕获子类异常再捕获父类异常


finally:不管是否发生了异常,都必须要执行的,常在finally中关闭程序块已打开的资源

注意事项

    1. 即使try和catch块中存在return语句,finally语句也会执行。是在执行完finally语句后再通过return退出。

    2. finally语句块只有一种情况是不会执行的,那就是在执行finally之前遇到了System.exit(0)结束程序运行。

 

throws: 在方法声明中抛出一个或多个异常对象


throw:抛出异常对象,只能跟一个



4. throws和throw的区别。

throws E1,E2,E3只是告诉程序这个方法可能会抛出这些异常,方法的调用者可能要处理这些异常,而这些异常E1,E2,E3可能是该函数体产生的。


throw则是明确了这个地方要抛出这个异常。

三、编码题

1. 编写程序接收用户输入分数信息,如果分数在0—100之间,输出成绩。如果成绩不在该范围内,抛出异常信息,提示分数必须在0—100之间。

要求:使用自定义异常实现。

package Exception;
import java.util.Scanner;

class MyException extends Exception{
//默认构造器
public MyException() {
}
//带有详细信息的构造器,信息存储在message
public MyException(String message) {
super(message);
}
}


class ScoreExpetion {
private int inscore;
public void setInscore(int inscore) throws MyException {
if(inscore<0){
throw new MyException("输入分数不能小于0");
}
if(inscore>100){
throw new MyException("输入分数不能大于0");
}
this.inscore=inscore;
}
}

public class ScoreException {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("请输入分数");
int score=input.nextInt();
ScoreExpetion se=new ScoreExpetion();
try {
se.setInscore(score);
System.out.println("分数为"+score);
} catch (MyException e) {
e.printStackTrace();
}
}
}

2. 写一个方法void isTriangle(int a,int b,int c),判断三个参数是否能构成一个三角形, 如果不能则抛出异常IllegalArgumentException,显示异常信息 “a,b,c不能构成三角形”,如果可以构成则显示三角形三个边长,在主方法中得到命令行输入的三个整数, 调用此方法,并捕获异常。

package Exception;


import java.util.Scanner;

public class Square {

//判断三个参数是否能构成一个三角形,如果不能则抛出异常
public void isTriangle(int a ,int b, int c){
if((a+b)<c){
throw new IllegalArgumentException("a,b,c不能构成三角形");
}
if((a+c)<b){
throw new IllegalArgumentException("a,b,c不能构成三角形");
}
if((b+c)<a){
throw new IllegalArgumentException("a,b,c不能构成三角形");
}
if(a<=0||b<=0||c<=0){
throw new IllegalArgumentException("a,b,c不能构成三角形");
}
System.out.println("三角形的各边长为"+a+","+b+","+c);
}

public static void main(String[] args){
System.out.println("请输入三角形的三个边长");
Scanner input=new Scanner(System.in);
Square square=new Square();
int a=input.nextInt();
int b=input.nextInt();
int c=input.nextInt();
square.isTriangle(a,b,c);
}
}

3. 编写一个计算N个学生分数平均分的程序。程序应该提示用户输入N的值,如何必须输入所有N个学生分数。如果用户输入的分数是一个负数,则应该抛出一个异常并捕获,提示“分数必须是正数或者0”。并提示用户再次输入该分数。

package Exception;

import java.util.Scanner;

public class Average {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("请输入学生的个数");
int studentNumber=input.nextInt();
ScoreExpetion se=new ScoreExpetion();
int[] studentScore=new int[studentNumber];
for(int i=0;i<studentScore.length;i++){
System.out.println("请输入"+(i+1)+"号学生的分数");
studentScore[i]=input.nextInt();
try {
se.setInscore(studentScore[i]);
} catch (MyException e) {
e.printStackTrace();
i--;
continue;
}
}
int sum=0;
for(int a:studentScore){
sum+=a;
}

System.out.println("平均分为"+(sum/studentNumber));
}

}

 

package Exception;

import java.util.Scanner;

class MyException extends Exception{
//默认构造器
public MyException() {
}
//带有详细信息的构造器,信息存储在message
public MyException(String message) {
super(message);
}
}


class ScoreExpetion {
private int inscore;

public void setInscore(int inscore) throws MyException {
if(inscore<0){
throw new MyException("输入分数不能小于0");
}
if(inscore>100){
throw new MyException("输入分数不能大于0");
}
this.inscore=inscore;
}
}

public class ScoreException {

public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("请输入分数");
int score=input.nextInt();
ScoreExpetion se=new ScoreExpetion();
try {
se.setInscore(score);
System.out.println("分数为"+score);
} catch (MyException e) {
e.printStackTrace();
}
}
}
posted @ 2019-04-24 19:09  L_Trouble  阅读(588)  评论(0编辑  收藏  举报