Exception获取getMessage()为空

e.getMessage(); 只会获得具体的异常名称. 比如说NullPoint 空指针,就告诉你说是空指针,使用是因为可以使日志简洁。

e.printStackTrace();(e.getStackTrace())会打出详细异常,异常名称,出错位置,便于调试用.. 一般一个异常至少几十行。
两者有不同的用处。
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我会回答你的问题和编写代码。以下是代码实现: EmptyStackException类: ```java public class EmptyStackException extends Exception { public EmptyStackException(String message) { super(message); } } ``` FullStackException类: ```java public class FullStackException extends Exception { public FullStackException(String message) { super(message); } } ``` IntStack类: ```java public class IntStack { private int[] stackArray; private int stackSize; private int top; public IntStack() { this(16); } public IntStack(int size) { if (size <= 0) { throw new IllegalArgumentException("Stack size must be a positive integer."); } stackSize = size; stackArray = new int[stackSize]; top = -1; } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (top == stackSize - 1); } public int pop() throws EmptyStackException { if (isEmpty()) { throw new EmptyStackException("Stack is empty."); } int value = stackArray[top]; top--; return value; } public void push(int value) throws FullStackException { if (isFull()) { throw new FullStackException("Stack is full."); } top++; stackArray[top] = value; } public int peek() throws EmptyStackException { if (isEmpty()) { throw new EmptyStackException("Stack is empty."); } return stackArray[top]; } public static void main(String[] args) { IntStack stack = new IntStack(5); try { stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.push(5); //stack.push(6);//测试FullStackException while (!stack.isEmpty()) { System.out.println(stack.pop()); } //System.out.println(stack.peek());//测试EmptyStackException } catch (FullStackException e) { System.out.println(e.getMessage()); } catch (EmptyStackException e) { System.out.println(e.getMessage()); } } } ``` 在主函数中,我们创建了一个大小为5的IntStack对象,并进行了一些push和pop操作,同时也测试了FullStackException和EmptyStackException的处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值