步骤:
1.创建异常类,继承Exception类;
2.在类中创建两个构造方法,一个带报错信息参数,一个不带;
3.带报错信息参数的构造方法要继承父类(Exception类)的构造方法,目的是 e.getMessage()和报异常时能获取报错信息
4.最后一层调用catch异常,之前的只要抛出异常
5.调用抛出异常的方法,必须throws该异常
6.第一次遇到判断异常的方法A,要new一个异常,才能抛出;继而,调用A的其他方法也有异常可以抛出
带报错信息参数的构造方法继承父类的构造方法:
不继承父类方法:
代码:
创建了IndexIsNagetiveException和IndexIsOutofRangeException两个自定义异常,用于insert时抛出负数位置和超界位置,并在打印异常时进行详细说明
package test;
public class MyStringBuffer implements IStringBuffer{
//自定义抛出异常
public class IndexIsNagetiveException extends Exception {
public IndexIsNagetiveException() {
}
public IndexIsNagetiveException(String message) {
super(message); //使e.getMessage()能获取message
}
}
public class IndexIsOutofRangeException extends Exception {
public IndexIsOutofRangeException() {
}
public IndexIsOutofRangeException(String message) {
super(message);
}
}
int capacity = 16;
int length = 0;
char[] value;
public MyStringBuffer(){
value = new char[capacity];
}
//有参构造方法
public MyStringBuffer(String str){
this();
if(null==str)
return;
if(capacity<str.length()){
capacity = value.length*2;
value=new char[capacity];
}
if(capacity>=str.length())
System.arraycopy(str.toCharArray(), 0, value, 0, str.length());
length = str.length();
}
@Override
public void append(String str) throws IndexIsNagetiveException, IndexIsOutofRangeException {
insert(length,str);
}
@Override
public void append(char c) throws IndexIsNagetiveException, IndexIsOutofRangeException {
append(String.valueOf(c));
}
@Override
public void insert(int pos, char b) throws IndexIsNagetiveException, IndexIsOutofRangeException {
insert(pos, String.valueOf(b));
}
@Override
public void delete(int start) {
delete(start,length);
}
@Override
public void delete(int start, int end) {
//边界条件判断
if(start<0)
return;
if(start>length)
return;
if(end<0)
return;
if(end>length)
return;
if(start>=end)
return;
System.arraycopy(value, end, value, start, length- end);
length-=end-start;
}
@Override
public void reverse() {
for (int i = 0; i < length/2; i++) {
char temp = value[i];
value[i] = value[length-i-1];
value[length-i-1] = temp;
}
}
@Override
public int length() {
// TODO Auto-generated method stub
return length;
}
@Override
public void insert(int pos, String b) throws IndexIsNagetiveException, IndexIsOutofRangeException {
//边界条件判断
if(pos<0){
throw new IndexIsNagetiveException("插入的位置是"+pos+",为负");
}
if(pos>length){
throw new IndexIsOutofRangeException("插入的位置是"+pos+",超越最大界:"+length);
}
if(null==b){
return;
}
//扩容
while(length+b.length()>capacity){
capacity = (int) ((length+b.length())*1.5f);
char[] newValue = new char[capacity];
System.arraycopy(value, 0, newValue, 0, length);
value = newValue;
}
char[] cs = b.toCharArray();
//先把已经存在的数据往后移
System.arraycopy(value, pos, value,pos+ cs.length, length-pos);
//把要插入的数据插入到指定位置
System.arraycopy(cs, 0, value, pos, cs.length);
length = length+cs.length;
}
public String toString(){
char[] realValue = new char[length];
System.arraycopy(value, 0, realValue, 0, length);
return new String(realValue);
}
public static void main(String[] args) {
MyStringBuffer sb = new MyStringBuffer("there light");
System.out.println(sb);
//抛出异常
try{
sb.insert(-1, "let ");
} catch (IndexIsNagetiveException | IndexIsOutofRangeException e) {
System.out.println("具体原因:"+e.getMessage());
e.printStackTrace();
}
}
}