9.4 Java exception stack trace

PrintStackTTrace exception object () method is used to print a stack trace of the exception, according to the printStackTTrace () method outputs the result, developers can find the source of the exception, and process tracking to trigger abnormal way.
The following test printStackTrace example:

class SelfException extends RuntimeException
{
	SelfException(){}
	SelfException(String msg)
	{
		super(msg);
	}
}
public class printStackTraceTest
{
	public static void main(String[] args)
	{
		firstMed();
	}

	public static void firstMed()
	{
		secondMed();
	}
	public static void secondMed()
	{
		thirdMed();
	}
	public static void thirdMed()
	{
		throw new SelfException("自定义异常信息");
	}
}

Run the above procedures can be seen in the results shown below:

From the start trigger can be seen from abnormal thirdMed method, spread method secondMed, then spread firstMed method, and finally spread to the main method, the main method terminates, the process Java is the exception stack trace.
Object-oriented application is running, often happens amount chair method calls, thus forming method call stack, abnormal spread of the opposite: as long as the exception is not fully captured (including exception is not caught, or an exception is thrown the new post-processing abnormal), starting from abnormal abnormal method of spread out, first to the caller method, the method caller again pass its caller until the main method of spread, if the main approach still does not handle the exception, JVM will terminate the program, and print a stack trace information.

Guess you like

Origin www.cnblogs.com/weststar/p/12629482.html