zoukankan      html  css  js  c++  java
  • 异常练习(自定义异常)

    练习一:

     1 package Exception;
     2 /**
     3  * 计算一个圆和长方形的面积?
     4  * 注意:面积不能为0和负数。请用异常来描述;
     5  * @author asus1
     6  *
     7  */
     8 //自定义一个异常
     9 class NovalueException extends RuntimeException{
    10     NovalueException(String mssage){
    11         super(mssage);
    12     }
    13 }
    14 public class ExceptionDemo3 {
    15 
    16     public static void main(String[] args) {
    17         try {
    18             ricImpl ri = new ricImpl(5,2);
    19             double riArea = ri.getArea();
    20             System.out.println(riArea);
    21             
    22             CirlcImpl ci = new CirlcImpl(-5);
    23             double ciArea = ci.getArea();
    24             System.out.println(ciArea);
    25             
    26         } catch (NovalueException e) {
    27             System.out.println(e.toString());
    28         }
    29         
    30     }
    31 
    32 }
    33 
    34 interface Shape {
    35     double getArea();
    36 }
    37 
    38 class ricImpl implements Shape{
    39     private double width;
    40     private double height;
    41     ricImpl(double width,double height) throws NovalueException{
    42         if(width <=0 || height<=0){
    43             throw new NovalueException("非法数据");
    44         };
    45         this.width = width;
    46         this.height = height;
    47     }
    48     public double getArea(){
    49         return width*height;
    50     }
    51 }
    52 class CirlcImpl implements Shape{
    53     private double radius;
    54     public static final double PI = 3.14;
    55     CirlcImpl(double radius) throws NovalueException{
    56         if(radius <=0){
    57             throw new NovalueException("圆--非法数据");
    58         }
    59         this.radius = radius;
    60     }
    61     public double getArea(){
    62         return radius*radius*PI;
    63     }
    64 }

    练习二:

     1 package Exception;
     2 /**
     3  * 老师讲课
     4  * 问题:1.电脑蓝屏了
     5  *         2.电脑冒烟了
     6  * 需求:解决以上两异常
     7  * @author asus1
     8  *
     9  */
    10 class lanpinException extends Exception{  //蓝屏异常
    11     lanpinException(String mssage){
    12         super(mssage);
    13     }
    14 }
    15 class maoyanException extends Exception{  //冒烟异常
    16     maoyanException(String mssage2){
    17         super(mssage2);
    18     }
    19 }
    20 public class Exception_lianxi {
    21 
    22     public static void main(String[] args) {
    23         Teacher t = new Teacher("童");
    24         t.prelect();
    25         System.out.println("讲课了");
    26     }
    27 
    28 }
    29 class Teacher{
    30     private String name;
    31     public Teacher(String name) {
    32         super();
    33         this.name = name;
    34     }
    35     public void prelect(){
    36         Complate co = new Complate();
    37         try {//正常
    38             co.run();
    39         } catch (lanpinException e) {  //蓝屏异常抛出
    40             //解决蓝屏异常的代码
    41             System.out.println(e.getMessage());
    42             co.reset();                
    43         }catch (maoyanException e) {  //冒烟异常抛出
    44             //解决冒烟异常的代码
    45             System.out.println(e.getMessage());
    46             gotolianxi lianxi = new gotolianxi();
    47             //lianxi.toString();
    48         }
    49         
    50     }
    51 }
    52 class gotolianxi{
    53     public gotolianxi(){
    54         System.out.println("去练习");
    55     }
    56 }
    57 class Complate{
    58     private int state = 3;  //1- 正常 ; 2- 蓝屏;  3- 冒烟;
    59     
    60     public void run() throws lanpinException,maoyanException{
    61         if(state == 1){
    62             System.out.println("runing!!");
    63         }
    64         if(state == 2){
    65             throw new lanpinException("电脑蓝屏了-");//抛出编译时检测的异常 需要在方法外声明定义这个异常
    66         }
    67         if(state == 3){
    68             throw new maoyanException("电脑冒烟了-");
    69         }
    70     }
    71     public void reset(){
    72         System.out.println("电脑重启ing");
    73     }
    74 }
  • 相关阅读:
    LeetCode Subsets II
    LeetCode Rotate Image
    LeetCode Palidrome Number
    LeetCode Generate Parentheses
    LeetCode Maximum Subarray
    LeetCode Set Matrix Zeroes
    LeetCode Remove Nth Node From End of List
    Linux Loop设备 使用
    Linux 文件系统大小调整
    LeetCode N-Queens II
  • 原文地址:https://www.cnblogs.com/tongxuping/p/6832575.html
Copyright © 2011-2022 走看看