zoukankan      html  css  js  c++  java
  • 异常02

     1 /*
     2  *  异常分为编译异常和运行时期异常
     3  *    编译异常: 调用了抛出异常的方法,不处理编译失败  (try  throws)
     4  *    运行异常: 抛出的异常是RuntimeException类,或者是他的子类
     5  *  
     6  *  运行异常的特点:
     7  *     方法内部抛出的异常是运行异常, new XXXException
     8  *     方法的声明上,不需要throws语句,调用者,不需要处理
     9  *     设计原因:
    10  *        运行异常,不能发生,但是如果发生了,程序人员停止程序修改源代码
    11  *        
    12  *        运行异常: 一旦发生,不要处理,请你修改源代码, 运行异常一旦发生,后面的代码没有执行的意义
    13  */
    14 public class RuntimeExceptionDemo {
    15     public static void main(String[] args) {
    16             double d = getArea(1);
    17             System.out.println(d);
    18     }
    19     
    20     /*
    21      *  定义方法,计算圆形的面积
    22      *  传递参数0,或者负数,计算的时候没有问题
    23      *  但是,违反了真实情况
    24      *  参数小于=0, 停止程序,不要在计算了
    25      */
    26     public static double getArea(double r){
    27         if(r <= 0)
    28             throw new RuntimeException("圆形不存在");
    29         return r*r*Math.PI;
    30     }
    31     
    32     
    33     public static void function(){
    34         int[] arr = {1,2,3};
    35         //对数组的5索引进行判断,如果5索引大于100,请将5索引上的数据/2,否则除以3
    36         //索引根本就没有
    37         if(arr[5] > 100){
    38             arr[5] = arr[5]/2;
    39         }else{
    40             arr[5] = arr[5]/3;
    41         }
    42     }
    43 }
     1 /*
     2  * Throwable类中的方法
     3  * 三个方法,都和异常的信息有关系
     4  *     String getMessage(); 对异常信息的详细描述
     5  *     String toString();   对异常信息的简短描述
     6  *     void printStackTrace(); 将异常信息追踪到标准的错误流;虚拟机JVM的默认调用方法;推荐用
     7  */
     8 public class Demo03 {
     9     public static void main(String[] args) {
    10         try {
    11             function();
    12         } catch (Exception e) {
    13             //System.out.println(e.getMessage());
    14             //System.out.println(e.toString());
    15             e.printStackTrace();
    16         }
    17     }
    18     public static void function()throws Exception{
    19         throw new Exception("异常了");
    20     }
    21 }

    自定义异常

     1 /*
     2  * 自定义异常
     3  *     继承Exception,或者继承RuntimeException
     4  *     构造方法,super将异常信息传递给父类
     5  */
     6 public class Demo04{
     7     public static void main(String[] args) {
     8         System.out.println(getAvg(99,-45,75));
     9     }
    10     public static int getAvg(int...source){
    11         int sum=0;
    12         for(int s:source){
    13             if(s<0)
    14                 throw new fushuException("负数异常!"+s);
    15             sum += s;
    16         }
    17         return sum/source.length;
    18     }
    19 }
    20 class fushuException extends RuntimeException{
    21     public fushuException(String s){
    22         super(s);
    23     }
    24     public fushuException(){}
    25 }
  • 相关阅读:
    hdu 1028 Ignatius and the Princess III (n的划分)
    CodeForces
    poj 3254 Corn Fields (状压DP入门)
    HYSBZ 1040 骑士 (基环外向树DP)
    PAT 1071 Speech Patterns (25)
    PAT 1077 Kuchiguse (20)
    PAT 1043 Is It a Binary Search Tree (25)
    PAT 1053 Path of Equal Weight (30)
    c++ 常用标准库
    常见数学问题
  • 原文地址:https://www.cnblogs.com/Nelsoner/p/6707282.html
Copyright © 2011-2022 走看看