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编写简单的自定义异常类
下一篇: laravel后台登录session验证
推荐阅读
-
Java kafka如何实现自定义分区类和拦截器
-
C#_Excel数据读取与写入_自定义解析封装类_支持设置标题行位置&使用excel表达式收集数据&单元格映射&标题映射&模板文件的参数数据替换(第二版-增加深度读取和更新功能)
-
Java自定义异常类
-
如何使用Serializable接口来自定义PHP中类的序列化
-
异常类的了解、异常的处理方案、编译时期异常和运行时期异常区别、throws和throw的区别、自定义异常类 、捕获异常的标准格式、final、finally、finalize的区别
-
自定义异常和throw、throws的使用
-
详述自定义异常类与throw和throws的区别
-
java_自定义异常类
-
Java自定义一个异常类NoThisSongException和Player类
-
Java 自定义异常类模拟用户注册成功与失败