zoukankan      html  css  js  c++  java
  • java学习之异常(Class7)

    一. Java中实现异常处理的基础知识。

    1.把可能会发生错误的代码放进try语句块中。

    当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。

    catch语句块中的代码用于处理错误。

    当异常发生时,程序控制流程由try语句块跳转到catch语句块。

    不管是否有异常发生,finally语句块中的语句始终保证被执行。

    如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

    2.java在异常处理的时候会尽可能catch更加具体的异常,一般不处理Exception类。

    3.自定义类时可以继承Exception类。

    二. 多层的异常捕获-1

     

    三. 多层的异常捕获-2

     

    四. finally语句块一定会执行吗?

    不会,如果用system.exit()函数退出,就不会执行

    五. 课后作业2

    1.源代码

      1 package after_class;
      2 
      3 import java.util.Scanner;
      4 
      5 class IsNumberException extends Exception
      6 {
      7     /**
      8      * 
      9      */
     10     private static final long serialVersionUID = 1L;
     11     IsNumberException()  
     12     {
     13         super();  
     14     }  
     15     IsNumberException(String msg)  
     16     { 
     17         super(msg);  
     18     }  
     19 }
     20 
     21 class OutofboudaryException extends Exception
     22 {
     23     /**
     24      * 
     25      */
     26     private static final long serialVersionUID = 1L;
     27     OutofboudaryException()  
     28     {
     29         super();  
     30     }  
     31     OutofboudaryException(String msg)  
     32     { 
     33         super(msg);  
     34     }  
     35 }
     36 
     37 public class Exam {
     38 
     39     public static void main(String[] args) 
     40     {
     41         Scanner in  = new Scanner(System.in);
     42         
     43         System.out.println("请输入学生的成绩:");
     44         String str = in.nextLine();
     45         
     46         if(str.isEmpty())
     47         {
     48             System.out.println("没有输入数值!");
     49             System.exit(1);
     50         }
     51         
     52         char contain[] = str.toCharArray();
     53         
     54         try {
     55             for(int i = 0;i<contain.length;i++)
     56             {
     57                 if(contain[i]<'0'||contain[i]>'9')
     58                 {
     59                     throw new IsNumberException("输入的值不符合要求");
     60                 }
     61             }
     62             }catch(IsNumberException e)
     63             {
     64                 System.out.println(e.getMessage().toString());
     65                 System.exit(1);
     66             }
     67         
     68         try {
     69             
     70             if(contain.length>3)
     71             {
     72                 System.out.println("1");
     73                 throw new OutofboudaryException("输入的值越界了");
     74             }
     75             else if(contain.length==3&&contain[0]=='1'&&contain[1]!='0'&&contain[2]!='0')
     76             {
     77                 System.out.println("2");
     78                 throw new OutofboudaryException("输入的值越界了");
     79             }
     80             else if(contain.length==3&&contain[0]>'1')
     81             {
     82                 System.out.println("3");
     83                 throw new OutofboudaryException("输入的值越界了");
     84             }
     85             
     86         }
     87         catch(OutofboudaryException e) {
     88             System.out.println(e.getMessage().toString());
     89             System.exit(1);
     90             }
     91             
     92         if(contain.length==3)
     93         {
     94             System.out.println("优");
     95         }
     96         else if(contain.length==2)
     97         {
     98             if(contain[0]=='9')
     99             {
    100                 System.out.println("优");
    101             }
    102             else if(contain[0]=='8')
    103             {
    104                 System.out.println("良");
    105             }
    106             else if(contain[0]=='7')
    107             {
    108                 System.out.println("中");
    109             }
    110             else if(contain[0]=='6')
    111             {
    112                 System.out.println("及格");
    113             }
    114             else
    115             {
    116                 System.out.println("不及格");
    117             }
    118         }
    119         else 
    120         {
    121             System.out.println("不及格");
    122         }
    123         
    124         in.close();
    125         
    126     }
    127 }
    128                 
    129         

    2.运行结果

  • 相关阅读:
    RequestMappin
    数组换位子
    mysql 数据表中查找重复记录(条数)
    post测试
    maven
    常用String练习
    删除重复数据
    推荐几个不错的jQuery图表插件,让你的报表更清晰动感
    纯CSS画的基本图形(矩形、圆形、三角形、多边形、爱心、八卦等),NB么?
    在中国,我们的知识产权真的陨落了吗?
  • 原文地址:https://www.cnblogs.com/tianxiayoujiu/p/7849531.html
Copyright © 2011-2022 走看看