欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java如何自定义异常类和自定义异常类的简单使用

程序员文章站 2022-03-15 11:43:11
...
package com.cdzyjsxy.javase.exception;

import com.sun.org.apache.xpath.internal.SourceTree;
import com.sun.org.apache.xpath.internal.objects.XObject;
import com.sun.xml.internal.ws.addressing.WsaActionUtil;

/**
 * @author FL
 * @date 2020/6/17
 **/
public class MyStack {

    private Object [] elements;
    private int index;

    public MyStack(){
        this.elements = new Object[10];
        this.index = -1;
    }
    /**
     * 亚栈方法
        * @param object
        * @return void
        * @author FanLin
        * @date 2020/6/17 9:44
    */
    public void push(Object object) throws MyStackOperationException {
        if (index>=elements.length-1){
//            System.out.println("占已满,亚栈失败");
//            return;
       //创建异常对象
       //     MyStackOperationException e = new MyStackOperationException("栈已满,压栈失败");
       //手动抛出异常
       //     throw e;
       //合并上述两行代码
            throw new MyStackOperationException("栈已满,压栈失败");
        }
        index++;
        elements[index] = object;
        System.out.println("亚栈"+object+"元素成功,栈针指向"+index);
    }
    /**
     * 弹栈方法
        * @param
        * @return void
        * @author FanLin
        * @date 2020/6/17 9:50
    */
    public void pop() throws MyStackOperationException {
        if (index<0){
//            System.out.println("弹栈失败,栈已空");
//            return;
//           创建异常对象
//         MyStackOperationException e =   new MyStackOperationException("弹栈失败,栈已空");
//           手动抛出异常
//         throw e;
     //合并上述两行代码
            throw new MyStackOperationException("弹栈失败,栈已空");

        }
        System.out.println("弹栈"+elements[index]+"元素成功");
        index--;
        System.out.println("栈针指向"+index);
    }

    public Object[] getElements() {
        return elements;
    }

    public void setElements(Object[] elements) {
        this.elements = elements;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}

package com.cdzyjsxy.javase.exception;

/**
 * @author FL
 * @date 2020/6/17
 **/
//栈操作异常:自定义异常
public class MyStackOperationException extends Exception {
    public MyStackOperationException() {
        super();
    }

    public MyStackOperationException(String message) {
        super(message);
    }
}

package com.cdzyjsxy.javase.exception;

/**
 * @author FL
 * @date 2020/6/17
 **/
//测试改过后的程序
public class Exception05 {
    public static void main(String[] args) {
       //创建对象
        MyStack myStack = new MyStack();
        //压栈
        try {
            myStack.push(new Object());
            myStack.push(new Object());
            myStack.push(new Object());
            myStack.push(new Object());
            myStack.push(new Object());
            myStack.push(new Object());
            myStack.push(new Object());
            myStack.push(new Object());
            myStack.push(new Object());
            myStack.push(new Object());
            //压栈失败
            myStack.push(new Object());
            myStack.push(new Object());

        } catch (MyStackOperationException e) {
            e.printStackTrace();
            System.out.println("shibai");
        }
        //弹栈
        try {
            myStack.pop();
            myStack.pop();
            myStack.pop();
            myStack.pop();
            myStack.pop();
            myStack.pop();
            myStack.pop();
            myStack.pop();
            myStack.pop();
            myStack.pop();
            //此处就应该弹栈失败
            myStack.pop();

        } catch (MyStackOperationException e) {
            e.printStackTrace();
            System.out.println("shibai");
        }
    }
}

 

相关标签: java