Tag: GetMessage () printstacktrace () Publicclassexceptiontest07{publicstaticvoidmain (String[]args) {try{ Fileinputstreamfis=newfileinputstream ("C:/abc.txt");//JVM executed this code for US// Filenotfoundexceptione=newfilenotfoundexception ("c:\abc.txt" (the system cannot find the specified file.) )");} catch (Filenotfoundexceptione) {//print exception stack information.//Generally use this method to debug the program.//e.printstacktrace ();/* java
stack Traces of Java exceptions (stack trace)
When an exception is caught, some processing is often required. The simpler and more straightforward way is to print the exception stack trace. Talking about the stack trajectory, probably a lot of people like me, the first reaction is the Printstacktrace () method. In fact, in addition to this method, there are some other content is related to the stack trajec
Usually write Java code, want to see the exception information thrown to find out the specific anomaly, we often use exception.tostring () or exception.getmessage () to get the exception information, and then print it to the console, But this information can only tell us the exception itself, and it's not ideal for us to find the anomaly, so we'll use the Exception.printstacktrace () method so we can output very detailed exception information at the c
try{...
}
catch (Exception e)
{
e.printstacktrace ();
}
When an exception occurs in a try statement, the statement in the catch is executed, and the Java Run-time system automatically initializes the exception e in the catch bracket, that is, instantiating the object of the exception type. E is the name of this object exception. Then e (reference) automatically invokes the method specified in the exception class, and there is a e.printstacktrace ();
usually Printstacktrace () can provide more information, we usually need its information to debug the program, but sometimes it is not convenient to direct output, we prefer to save the error information to the log, so we need to put the. pringstacktrance () content programming string, How do you do that?
It's actually very simple:
StringWriter SW = new StringWriter ();
PrintWriter pw = new PrintWriter (SW);
E.printstacktrace (PW);
String ms
each time.This way, referring to the jdk1.6.0 version of the API documentation, find the Printstacktrace () method of the exception class and see the first sentence of Printstacktrace () in its parent class Throwable:
Output this throwable and its trace to the standard error stream. This method outputs the stack trace of this Throwable object to the error output stream as the value of the field System.err.
Get exception information e. printstacktrace ()
The operation logs need to be recorded during the recent project, but the exception information is found to use E. getmessage () cannot meet the requirements at all, and E. getmessage () Sometimes the obtained information does not know the specific error information, so we need to obtain e. content of printstacktrace ()
Get Exception error information
Package
E. printstacktrace () is usually printed on the console. However, it is not easy to view the stack content when the program goes online. Many or few items are printed in the production environment, second, you may not be able to directly view the content. In this case, you need to record the content, for example, to the database. The following method can be fully recorded. Java code
public static void main(
When an exception is thrown, there are many methods. First, we will introduce two commonly used methods.I used system. Out. println (E );This method prints an exception and outputs the exception, but it is different from another E. printstacktrace () method. The latter also prints an exception, but it also displays a deeper call Information.For example:A extends ---> B extends ----> CWhen a problem occurs during the creation of a, an exception is thro
Public classPrintstacktrace { Public Static voidmyprintstacktrace (throwable th) {System. out. println (TH); //System.err.println (Th.getclass (). GetName () + ":" +th.getmessage ());stacktraceelement[] St =Th.getstacktrace (); for(stacktraceelement s:st) System. out. println ("\tat"+s.getclassname () +"."+s.getmethodname () +"("+s.getfilename () +":"+s.getlinenumber () +")"); }}This method prints after the order will change this is related to IO flow, but also to study the source code-
Linux Kernel stack call Implementation Analysis-save_stack_trace, printstacktrace1 kernel thread
The kernel allocates 8 K stack space for each thread, and a struct thread_info struct is placed at the top of each stack to save thread-related
Java tutorial translation Sequence Java Introduction Build a JSE development environment-install JDK and eclipse Language basics Java Hello World Program Analysis Variable Java Variables Java Native type Conversion of Java
processing, you can continue the subsequent operations.
4. Notes for Java Exception HandlingThe rational use of the Java exception mechanism can make the program robust and clear, but unfortunately, the Java exception handling mechanism is often used incorrectly. The following are some notes about exceptions:
1. Do not ignore checked exceptionSee the following c
the finally block, the exception captured by the try block cannot be thrown. The exception caught by the outside is the exception information in the finally block, the real exception stack information in the try block is lost. See the following code:
Connection con = null; try {con = dataSource. getConnection ();......} Catch (SQLException e ){...... Throw e; // after some processing, the database exception is thrown to the caller for processing} finally {try {con. close ();} catch (SQLExceptio
); is = new FileInputStream (src ); // 3. The operation continuously reads the buffer array byte [] car = new byte [20]; int len = 0; // receive the actual read size // read the while (-1! = (Len = is. read (car) {// convert the output byte array to a String (decoded by the default Character Set) String info = new String (car, 0, len); System. out. print (info) ;}} catch (FileNotFoundException e) {e. printStackTrace (); System. out. println ("file doe
Using java redis to implement redis Message Queue using jedisApplication scenarios
Recently, I am working on a project in the company and need to store chat content. Considering the high I/O connections and frequent connections in the database, I decided to use the cache.
I learned from the Internet that redis can store binary data for all content, while java can serialize all objects. The serialization met
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.