当前位置: 移动技术网 > IT编程>开发语言>Java > 高淇java300集异常机制作业

高淇java300集异常机制作业

2019年04月25日  | 移动技术网IT编程  | 我要评论

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();
}
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网