Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 389|回复: 0

java 实验4 异常

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-9-3 09:58:04 | 显示全部楼层 |阅读模式

    异常(实际使用直接try-catch)

    1.常见系统异常

    异常

    异常的解释

    ClassNotFoundException

    未找到要装载的类

    ArrayIndexOutOfBoundsException

    数组访问越界

    FileNotFoundException

    文件找不到

    IOException

    输入、输出错误

    NullPointerException

    空指针访问

    ArithmeticException

    算术运算错误,如除数为0

    NumberFormatException

    数字格式错误

    InterruptedException

    中断异常。 

    2.自定义异常

    1)定义异常类—继承Exception类

    2)在方法内抛出异常

          throw new 异常类();

    3)声明方法存在异常

         在方法头的尾部加上:throws 异常类列表

     

    3.实战

    1.假设学校开设舞蹈课程,输入男生、女生的数量,如果男生或女生的数量为0,则舞蹈课程无法开设,如果不为0,则根据男生/女生人数的多少计算男生/女生的partner数量。请用异常类处理数量为0的情况,重新编写下面的代码。

    要求运行结果如下:

     

     

     主函数

    public class DancerLesson {
        public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
            try{
                System.out.println("Enter number of male dancers:");
                int men=input.nextInt();
                System.out.println("Enter number of female dancers:");
                int women=input.nextInt();
                if(men==0||women==0)
                    throw new myExcpetion(men,women);
                else
                {
                    if(men>=women)
                        System.out.println("Each woman must dance with"+men/(double)women+"men.");
                    else
                        System.out.println("Each man must dance with"+women/(double)men+"women.");
                    System.out.println("Begin the lesson");
                }
            }catch(myExcpetion e) {
                System.out.println(e);
            }
        }
    }

    异常类

    public class myExcpetion extends Exception {
        public String tostring() {
            return "人数错误";
        }
    
        public myExcpetion(int men,int women) {
            System.out.print("Lesson is canceled.");
            if(men==0&&women==0)
                System.out.println("No student");
            else if(men>0&&women==0)
                System.out.println("No men");
            else
                System.out.println("No women");
       
    

     结果

     

    2.要求声明定义两个Exception的异常子类:NoLowerLetter类和NoDigit类

    再声明一个People类,该类中的void printLetter(char c)方法抛出NoLowerLetter异常类对象,void

    printDigit(char c)方法抛出NoDigit异常类对象。请补充下列代码中【代码1】到【代码6】

    //ExceptionExample.java
    public class NoLowerLetter  // 类声明,声明一个Exception的子类NoLowerLetter
    {
        public void print()  {
           System.out.printf("%c",'#');
        }
    }
    public class NoDigit extends Exception  // 类声明,声明一个Exception的子类NoDigit
    {
        public void print()  {
           System.out.printf("%c",'*');
        }
    }
    class People  {
      void  printLetter(char c) throws NoLowerLetter {
          if(c<'a'||c>'z') {
               NoLowerLetter noLowerLetter= new NoLowerLetter(); // 创建NoLowerLetter类型对象
              throw noLowerLetter;  // 抛出noLowerLetter
          }
          else {
               System.out.print(c);
          }
       }
       void  printDigit(char c) throws NoDigit {
          if(c<'1'||c>'9') {
               NoDigit noDigit= new NoDigit();  // 创建NoDigit()类型对象
                 throw noDigit;                 // 抛出noDigit
          }
          else {
               System.out.print(c);
          }
       }
    }
    public class ExceptionExample {  
        public static void main (String args[ ]){
            People people=new People( );
            for(int i=0;i<128;i++) {   
                  try
                     {
                       people.printLetter((char)i);
                     }
                   catch(NoLowerLetter e)
                     {
                        e.print();
                     }
            }
            for(int i=0;i<128;i++) {  
                   try
                     {
                       people.printDigit((char)i);
                     }
                   catch(NoDigit e)
                     {
                        e.print( );
                     }
             }
         }
    }

     

    3.选择题

    1)设有如下代码:                                                       

     try { 
         tryThis(); 
         return; 
     } catch (IOException x1) { 
         System.out.println("exception 1"); 
         return; 
     } catch (Exception x2) { 
         System.out.println("exception 2"); 
         return; 
     } finally { 
         System.out.println("finally");
     } 

     

    如果tryThis() 抛出 NumberFormatException,则输出结果是? (C)

    A. 无输出

    B. "exception 1", 后跟 "finally"

    C. "exception 2", 后跟 "finally"

    D. "exception 1"

    E. "exception 2"

     

    2)设有如下方法:

    public void test() {
           try { oneMethod();
                System.out.println("condition 1");
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("condition 2");
            } catch(Exception e) {
                System.out.println("condition 3");
           } finally {
                System.out.println("finally");
            }
        }

     

    如果oneMethod正常运行,则输出结果中有哪些?(AD)

    A. condition 1

    B. condition 2

    C. condition 3

    D. finally

     

    3) 设有如下代码:

    public void fun () {
        int i;
        try
        {
              i=System.in.read ();
              System.out.println("Location 1");
         } catch (IOException e) {
                System.out.println("Location 2");
         } finally {
               System.out.println("Location 3");
           }
           System.out.println("Location 4");
    }

     

    如果有一个IOException发生, 则输出有哪些?    (BCD)

    A. Location 1

    B. Location 2

    C. Location 3

    D. Location 4

    4)  设有如下代码:

    1 String s = null
    2 if ( s != null & s.length() > 0) 3 System.out.println("s != null & s.length() > 0"); 4 if ( s != null && s.length() > 0) 5 System.out.println("s != null & s.length() > 0"); 6 if ( s != null || s.length() > 0) 7 System.out.println("s != null & s.length() > 0"); 8 if ( s != null | s.length() > 0) 9 System.out.println("s != null | s.length() > 0");

     

    以下行中哪些会产生空指针异常。                (D)

    A. 2,4

    B. 6,8

    C. 2,4,6,8

    D. 2,6,8

     

     

    5) 类Test1、Test2定义如下:

    1.public class  Test1 {
    2.   public  float  aMethod(float a,float b) throws IOException {
    3.   }
    4. }
    5. public  class  Test2  extends  Test1{
    67. 

     

    将以下哪种方法插入行6是不合法的。              (A)

    A、float  aMethod(float  a,float  b){ }

    B、public  int  aMethod(int a,int b)throws  Exception{ }

    C、public  float  aMethod(float  p,float q){ }

    D、public  int  aMethod(int a,int  b)throws IOException{ }

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-19 10:38 , Processed in 0.075949 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表