ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

第五次实训作业异常处理

2019-06-10 21:01:15  阅读:193  来源: 互联网

标签:String 作业 System 第五次 实训 println cause public out


  1. 编写一个类ExceptionTest,在main方法中使用try-catch-finally语句结构实现:

² 在try语句块中,编写两个数相除操作,相除的两个操作数要求程序运行时用户输入;

² 在catch语句块中,捕获被0除所产生的异常,并输出异常信息;

在finally语句块中,输出一条语句。

package shixun;

import java.util.Scanner;

public class ExceptionTest {

public static void main(String[] args) {
    // TODO 自动生成的方法存根
    Scanner input = new Scanner(System.in);
    System.out.println("请输入除数:");
    int op1 = input.nextInt();
    System.out.println("请输入被除数:");
    int op2 = input.nextInt();
    int result = 0;
    try {
        result = op1 / op2;
    } catch (ArithmeticException e) {
        e.printStackTrace();
    } finally {
        System.out.println("异常处理");
    }
    System.out.println(result);

}

}

  1. 编写一个应用程序,要求从键盘输入一个double型的圆的半径,计算并输出其面积。测试当输入的数据不是double型数据(如字符串“abc”)会产生什么结果,怎样处理。

package yic;

import java.util.Scanner;

public class shiyan1 {

@SuppressWarnings("resource")
public static void main(String[] args) {
    // TODO Auto-generated method stub
    double r;
    Scanner s = new Scanner(System.in);
    while(true){
        try{
        r = Double.parseDouble(s.next());
        if(r<0)
            throw new NegativeException();
        else if(r<1.0||r>100.0)
            throw new NumberRangeException();
        else{
            System.out.println("面积是"+3.14*r*r);
            break;
        }
        }catch(NumberFormatException e){
            System.out.println("非数值异常");
        }catch(NegativeException e){
            System.out.println("负数异常");
        }catch(NumberRangeException e){
            System.out.println("越界异常");
        }
    }
}

}
class NegativeException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
public NegativeException(){
super();
}
public NegativeException(String msg){
super(msg);
}
public NegativeException(String msg, Throwable cause){
super(msg,cause);
}
public NegativeException(Throwable cause){
super(cause);
}
}
class NumberRangeException extends Exception{
public NumberRangeException(){
super();
}
public NumberRangeException(String msg){
super(msg);
}
public NumberRangeException(String msg, Throwable cause){
super(msg,cause);
}
public NumberRangeException(Throwable cause){
super(cause);
}
}

3.为类的属性“身份证号码.id”设置值,当给的的值长度为18时,赋值给id,当值长度不是18时,抛出IllegalArgumentException异常,然后捕获和处理异常,编写程序实现以上功能。

package yic;

public class shiyan2 {
private String id;
public void setid(String id) {
if(id.length()==18)
this.id=id;
else
throw new IllegalArgumentException("参数长度应为18!");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
shiyan2 t =new shiyan2();
try {
t.setid("0123456789123456789");
}catch(IllegalArgumentException ie) {
System.out.println(ie.getMessage());
}finally {
System.out.println("结束");
}

}

}

标签:String,作业,System,第五次,实训,println,cause,public,out
来源: https://www.cnblogs.com/minky/p/11000196.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有