java源码阅读 Throwable

package com.wang.study;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AboutThrowableStudy {
	private static class HelloException extends Exception {
		private static final long serialVersionUID = -3766786307167910755L;

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

	private static class HelloRunTimeException extends RuntimeException {
		private static final long serialVersionUID = 8382275306821095036L;

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

	private static class HelloThrowable extends Throwable {
		private static final long serialVersionUID = 5404125283659042876L;

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

	private static void exceptionStudy() {
		try {
			throw new HelloException("hello");
		} catch (HelloException e) {
			System.out.println(e.getClass().getName() + "---" + e + "---");
		}
	}

	public static void main(String[] args) throws Exception {
		//some others i do not like to write some example.
	}

	private static String tryCatchStudy(String string) {
		try {
			if (string.equals("hello")) {
				throw new Exception("hello");
			}
		} catch (Exception e) {
			System.out.println(e.getClass().getName() + "----" + e.getMessage());
			return "exception";
		}
		return string;
	}

	// if exception it will down
	private static String trywithoutCatchStudy(String tokenString) throws Exception {
		if (tokenString.equals("hello")) {
			throw new Exception("exception");
		}
		return tokenString;
	}

	private static void runtimeExceptionStudy() {
		try {
			throw new HelloRunTimeException("Hello run time exception");
		} catch (HelloRunTimeException e) {
			System.out.println("-----run time exception " + e.getClass().getSimpleName() + "------");
		}
	}

	// 出现异常如果不捕获会中止
	private static void runtimeExceptionWithoutTryCatch() {
		throw new HelloRunTimeException("Hello run time exception");
	}

	private static void thowableStudy() throws IOException {
		System.out.println("-----throwable test start-----");
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			try {
				String line = "";
				while ((line = bufferedReader.readLine()) != null) {
					System.out.println("----" + line + "-------");
					if (line.equals("hello")) {
						throw new HelloThrowable("hello is be put int to console");
					}
				}
			} catch (HelloThrowable e) {
				System.out.println("-----I have counter the message" + "hello" + e.toString() + "-----");
				break;
			}
		}
	}
}

从源代码看Throwable

Throwable是所有java异常和错误的父类。

   public void printStackTrace() {
        printStackTrace(System.err);
    }
    public void printStackTrace(PrintStream s) {
        printStackTrace(new WrappedPrintStream(s));
    }    
public void printStackTrace(PrintWriter s) {
        printStackTrace(new WrappedPrintWriter(s));
    }
    private void printStackTrace(PrintStreamOrWriter s) {
        // Guard against malicious overrides of Throwable.equals by
        // using a Set with identity equality semantics.
        Set<Throwable> dejaVu =
            Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
        dejaVu.add(this);

        //这里锁住了
        synchronized (s.lock()) {
            // Print our stack trace
            s.println(this);
            StackTraceElement[] trace = getOurStackTrace();//取出程序的栈信息
            for (StackTraceElement traceElement : trace)
                s.println("\tat " + traceElement);


            // Print suppressed exceptions, if any
            for (Throwable se : getSuppressed())
                se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);


            // Print cause, if any
            Throwable ourCause = getCause();
            if (ourCause != null)
                ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
        }
    }
    private synchronized StackTraceElement[] getOurStackTrace() {
        // Initialize stack trace field with information from
        // backtrace if this is the first call to this method
        if (stackTrace == UNASSIGNED_STACK ||
            (stackTrace == null && backtrace != null) /* Out of protocol state */) {
            int depth = getStackTraceDepth();//获取栈的深度
            stackTrace = new StackTraceElement[depth];
            for (int i=0; i < depth; i++)
                stackTrace[i] = getStackTraceElement(i);//获取栈的元素
        } else if (stackTrace == null) {
            return UNASSIGNED_STACK;
        }
        return stackTrace;
    }
ative int getStackTraceDepth();
native StackTraceElement getStackTraceElement(int index);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.awt.Component; import javax.swing.JOptionPane; /** * This class ExceptionManager and its subclasses are a form of * Exception. It is used to wrap all the Throwable instances * and handle them in a unified way. It will show the information which consists of * StackTraces and Messages by using JOptionPanel. * * @author Estelle * @version 1.0 * @see java.lang.Exception * @since jdk 1.5 */ public class ExceptionManager extends RuntimeException { private static final long serialVersionUID = -6963187366089365790L; /** * This field alerter is used to show the information the Class offered. * * @see javax.swing.JOptionPane */ private JOptionPane alerter; /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(String msg). * * @param msg The message will pass the specified constructor * @return An instance of the ExceptionManager created by invoking the constructor * ExceptionManager(String msg). */ public static ExceptionManager wrap(String msg){ return new ExceptionManager(msg); } /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(Throwable throwable). * * @param throwable The cause will pass the specified constructor * @return An instance of the ExceptionManager created by invoking the constructor * ExceptionManager(Throwable throwable). */ public static ExceptionManager wrap(Throwable throwable){ return new ExceptionManager(throwable); } /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(String msg,Throwable throwable). * * @param msg The message will pass the specified constructor * @param throwable The cause will pass the specified c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值