Java异常

2018-07-16  本文已影响54人  WilsonMing
Java异常

对比Exception和Error。

Exception和Error

Exception和Error都继承Throwable,

运行异常与一般异常区别

Eeception又分为可检测异常(Checked Exception)和运行异常(也可叫不可检查异常,Runtime Exception)。

ClassNotFoundException和NoClassDefFoundError有什么区别

throw和throws

 public void custormException()throws  CustomException{
        throw new CustomException("抛出CustormException");
    }

异常处理几点建议

try {
    int i=0;
    while (true) {
        arr[i]=0;
        i++;
    }
} catch (IndexOutOfBoundsException e) {
}
public E get(int index) {
    try {
        return listIterator(index).next();
    } catch (NoSuchElementException exc) {
        throw new IndexOutOfBoundsException("Index: "+index);
    }
}
try {
    ...
} catch (SomeException e) {
}
try {
    ...
} catch (SomeException e) {
System.out.println("12344");
}

自定义异常

一般根据项目需要自定义异常,特别是服务端处理业务的时候。一般自定义异常继承Exception,Throwable(因为Error一般用于JVM的异常错误,不常用自定义),因为异常已实现相关方法,只需重写构造方法就可以。代码如下:

public class CustomException extends Exception {
    public CustomException() {
    }

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

    public CustomException(String message, Throwable cause) {
        super(message, cause);
    }

    public CustomException(Throwable cause) {
        super(cause);
    }

    public CustomException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
public class TestException {
    public void testMethod(){
        try {
            custormException();
        } catch (CustomException e) {
            e.printStackTrace();
        }
    }
    public void custormException()throws  CustomException{
        throw new CustomException("抛出CustormException");
    }
    public static void main(String[] args) {
        TestException testException=new TestException();
        testException.testMethod();
    }
}

输出

com.mysiga.learn.throwable.CustomException: 抛出CustormException
    at com.mysiga.learn.throwable.TestException.custormException(TestException.java:15)
    at com.mysiga.learn.throwable.TestException.testMethod(TestException.java:9)
    at com.mysiga.learn.throwable.TestException.main(TestException.java:19)

public class CustomThrowable extends Throwable {
    public CustomThrowable() {
    }

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

    public CustomThrowable(String message, Throwable cause) {
        super(message, cause);
    }

    public CustomThrowable(Throwable cause) {
        super(cause);
    }

    public CustomThrowable(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
public class TestThrowable {
    public void testMethod(){
        try {
            custormException();
        } catch (CustomThrowable e) {
            e.printStackTrace();
        }
    }
    public void custormException()throws  CustomThrowable{
        throw new CustomThrowable("抛出CustomThrowable");
    }
    public static void main(String[] args) {
        TestThrowable testThrowable=new TestThrowable();
        testThrowable.testMethod();
    }
}

输出

com.mysiga.learn.throwable.CustomThrowable: 抛出CustomThrowable
    at com.mysiga.learn.throwable.TestThrowable.custormException(TestThrowable.java:15)
    at com.mysiga.learn.throwable.TestThrowable.testMethod(TestThrowable.java:9)
    at com.mysiga.learn.throwable.TestThrowable.main(TestThrowable.java:19)

上一篇 下一篇

猜你喜欢

热点阅读