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);