第六次上机

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

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

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

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

package 异常;

import java.util.Scanner;

public class Exceptiontset {

 public static void main(String[] args) {

   Scanner x=new Scanner(System.in);      

try{    

int  m,n;     

  System.out.println("请输入两个数:");          

m=x.nextInt();        

   n=x.nextInt();     

  System.out.println(m/n);      

}     

  catch(Exception e){    

    e.printStackTrace();    

   }             

      finally{    

    System.out.println("false");     

  }  

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

package 异常; import java.util.Scanner; public class Xiu {  

 public static void main(String[] args) {  

 Scanner x=new Scanner(System.in);  

 try{   

double i,PI=3.14;     

i=x.nextDouble();    

  System.out.println(i*i*PI);

 }

 catch(Exception e){  

 // e.printStackTrace();

  System.out.println("输入不合法:");

 }   

   finally{  

     System.out.println();   

   }

 }

}

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

package 异常;

public class Id {     static int avg(int number)throws IllegalArgumentException{           }  public static void main(String[] args) {   

 }

}

原文地址:https://www.cnblogs.com/qq602671387/p/10843600.html