java 自定义异常


java 自定义异常

 

 

**********************

相关类与接口

 

Throwable

​
public class Throwable implements Serializable {

    private static final long serialVersionUID = -3042686055658047285L;

    private transient int depth;
    private transient Object backtrace;
    private String detailMessage;
    private Throwable cause = this;

    private StackTraceElement[] stackTrace = UNASSIGNED_STACK;
    private static final StackTraceElement[] UNASSIGNED_STACK = new StackTraceElement[0];

    private static final Throwable[] EMPTY_THROWABLE_ARRAY = new Throwable[0];
    private List<Throwable> suppressedExceptions = SUPPRESSED_SENTINEL;
    private static final List<Throwable> SUPPRESSED_SENTINEL = Collections.emptyList();

    private static final String NULL_CAUSE_MESSAGE = "Cannot suppress a null exception.";
    private static final String SELF_SUPPRESSION_MESSAGE = "Self-suppression not permitted";

    private static final String CAUSE_CAPTION = "Caused by: ";
    private static final String SUPPRESSED_CAPTION = "Suppressed: ";


*********
构造方法

    public Throwable() {
    public Throwable(String message) {
    public Throwable(String message, Throwable cause) {
    public Throwable(Throwable cause) {

    protected Throwable(String message, Throwable cause,
                        boolean enableSuppression,
                        boolean writableStackTrace) {


*********
普通方法

    public String getMessage() {
    public String getLocalizedMessage() {

    public synchronized Throwable getCause() {
    public synchronized Throwable initCause(Throwable cause) {
    final void setCause(Throwable t) {

    public void printStackTrace() {
    public void printStackTrace(PrintStream s) {
    public void printStackTrace(PrintWriter s) {
    private void printStackTrace(PrintStreamOrWriter s) {
    private void printEnclosedStackTrace(PrintStreamOrWriter s,
                                         StackTraceElement[] enclosingTrace,
                                         String caption,
                                         String prefix,
                                         Set<Throwable> dejaVu) {


    public final synchronized void addSuppressed(Throwable exception) {
    public final synchronized Throwable[] getSuppressed() {

    public synchronized Throwable fillInStackTrace() {
    private native Throwable fillInStackTrace(int dummy);

    public void setStackTrace(StackTraceElement[] stackTrace) {
    public StackTraceElement[] getStackTrace() {
    private synchronized StackTraceElement[] getOurStackTrace() {


    private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    private synchronized void writeObject(ObjectOutputStream s) throws IOException {
    private int validateSuppressedExceptionsList(List<Throwable> deserSuppressedExceptions) throws IOException {

    public String toString() {


*********
静态内部类:Throwable.SentinelHolder

    private static class SentinelHolder {

        public static final StackTraceElement STACK_TRACE_ELEMENT_SENTINEL =
            new StackTraceElement("", "", null, Integer.MIN_VALUE);

        public static final StackTraceElement[] STACK_TRACE_SENTINEL =
            new StackTraceElement[] {STACK_TRACE_ELEMENT_SENTINEL};
    }


*********
静态内部类:Throwable.PrintStreamOrWriter

    private abstract static class PrintStreamOrWriter {

        abstract Object lock();
        abstract void println(Object o);
    }


*********
静态内部类:Throwable.WrappedPrintStream

    private static class WrappedPrintStream extends PrintStreamOrWriter {
        private final PrintStream printStream;

        WrappedPrintStream(PrintStream printStream) {
            this.printStream = printStream;
        }

        Object lock() {
            return printStream;
        }

        void println(Object o) {
            printStream.println(o);
        }
    }


*********
静态内部类:Throwable.WrappedPrinterWriter

    private static class WrappedPrintWriter extends PrintStreamOrWriter {
        private final PrintWriter printWriter;

        WrappedPrintWriter(PrintWriter printWriter) {
            this.printWriter = printWriter;
        }

        Object lock() {
            return printWriter;
        }

        void println(Object o) {
            printWriter.println(o);
        }
    }

​

 

Error:虚拟机错误,如内存溢出(outOfMemoryError等)

​
public class Error extends Throwable {
    @java.io.Serial
    static final long serialVersionUID = 4980196508277280342L;


*********
构造函数

    public Error() {
    public Error(String message) {
    public Error(String message, Throwable cause) {
    public Error(Throwable cause) {

    protected Error(String message, Throwable cause,
                    boolean enableSuppression,
                    boolean writableStackTrace) {

​

 

Exception:程序异常,可直接在应用程序中处理,需用try-catch语句块捕获

public class Exception extends Throwable {
    @java.io.Serial
    static final long serialVersionUID = -3387516993124229948L;


**********
构造函数

    public Exception() {
    public Exception(String message) {
    public Exception(String message, Throwable cause) {
    public Exception(Throwable cause) {

    protected Exception(String message, Throwable cause,
                        boolean enableSuppression,
                        boolean writableStackTrace) {

 

RuntimeException:程序运行时异常,可不捕获,直接由虚拟机处理

public class RuntimeException extends Exception {
    @java.io.Serial
    static final long serialVersionUID = -7034897190745766939L;


**********
构造函数

    public RuntimeException() {
    public RuntimeException(String message) {
    public RuntimeException(String message, Throwable cause) {
    public RuntimeException(Throwable cause) {

    protected RuntimeException(String message, Throwable cause,
                               boolean enableSuppression,
                               boolean writableStackTrace) {

 

 

**********************

示例:自定义异常

 

class CustomException extends RuntimeException{

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

public class MtTest2 {

    public static void main(String[] args){
        try {
            throw new CustomException("自定义异常");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

****************

控制台输出

 

trytest.CustomException: 自定义异常
	at trytest.MtTest2.main(MtTest2.java:14)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值