• Java 自定义异常


    Goods.java

    package ligong.sdut.demo;
    
    public class Goods {
           boolean isDanger;
           String name;
           public void setIsDanger(boolean boo) {
              isDanger = boo;
           }
           public boolean isDanger() {
              return isDanger;  
           }
           public void setName(String s) {
              name = s;
           }
           public String getName() {
              return name;
           }
        }

    Machine.java

    package ligong.sdut.demo;
    
    public class Machine {
    
        public void checkBag(Goods goods)  throws DangerException {
            // TODO Auto-generated method stub
            if (goods.isDanger) {   
    
               throw new DangerException();         
    
            }
        }
    }
    

    DangerException.java

    package ligong.sdut.demo;
    
    public class DangerException extends Exception{
    
        String message;
    
        public DangerException() {
            super();
            this.message = "危险品";
        };
        public void toShow() throws Exception{
            System.out.println(message+"");
        }
    
    }
    
    Check.java
    package ligong.sdut.demo;
    
    public class Check {
         public static void main(String args[]) throws Exception {
              Machine machine = new Machine();
              String name[] ={"苹果","炸药","西服","硫酸","手表","硫磺"};
              Goods [] goods = new Goods[name.length]; //检查6件物品  
              for(int i= 0;i<name.length;i++) {
                 goods[i] = new Goods();
                 if(i%2==0) {
                    goods[i].setIsDanger(false);
                    goods[i].setName(name[i]);
                 }
                 else {
                    goods[i].setIsDanger(true);
                    goods[i].setName(name[i]);
                 } 
              }
              for(int i= 0;i<goods.length;i++) {
                  try {
                      machine.checkBag(goods[i]);
                      System.out.println(goods[i].getName()+"检查通过");
                } catch (DangerException e) {
                    e.toShow();
                      System.out.println(goods[i].getName()+"检查不能通过");
                }
    
    
    
    
              }     
           } 
    }
    
  • 相关阅读:
    Windows监视器SSM应用手册
    300 Professional WordPress Themes Of 2012
    android TextWatcher
    Android save pictrue by SQLiteOpenHelper
    Android Animation
    android getResources
    我会 背诗哦
    android:imeOptions
    android ViewStub Note
    Android log analysis
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781758.html
Copyright © 2020-2023  润新知