java在写代码的过程中,什么时候可以抛异常反应代码运行过程的问题或者情况?
/**
* 删除栈顶元素
* @return
*/
public int pop(){
if(isEmpty()) {
throw new EmptyStackException("栈为空了!");
}
int oldVal = elem[usedSize-1];
usedSize--;
return oldVal;
}
public class EmptyStackException extends RuntimeException{
public EmptyStackException() {
}
public EmptyStackException(String msg) {
super(msg);
}
}
// 新增元素,默认在数组最后新增
public void add(int data) {
if(isFull()) {
elem = Arrays.copyOf(elem,2*elem.length);
}
/*
try {
if(isFull()) {
elem = Arrays.copyOf(elem,-1);
}
}catch (NegativeArraySizeException e) {
e.printStackTrace();
elem = Arrays.copyOf(elem,2*elem.length);
}*/
elem[usedSize++] = data;
}
public boolean isFull() {
//这里usedSize 必须和 elem.length
return usedSize == elem.length;
}
try {
if(isFull()) {
elem = Arrays.copyOf(elem,-1);
}
}catch (NegativeArraySizeException e) {
e.printStackTrace();
elem = Arrays.copyOf(elem,2*elem.length);
}
这个异常是 Arrays 自带的异常
如何查找该异常?
那不如先写个错误代码,就会编译器直接就会报异常