Introduction to Java Nineth: exceptions and log4j use

Source: Internet
Author: User
Tags throwable true true try catch

[Java Exceptions]The base class for all exceptions and errors in 1.java is throwable** throwable** error exception* (check-in exception) (Runtime exception) * * Exceptions in 2.java are divided into runtime exceptions and check-in exceptions. * Run-time exceptions represent runtimeexception and all subclasses, which do not need to be captured in the program, and most can be controlled through code to avoid avoidance. * Check for exceptions except for RuntimeException and all of its subclasses, requires that processing be captured in the program. * * 3. Custom exception classes, which must inherit from an existing exception class, typically inherit exception or runtimeexception*, respectively, to declare a check-time exception and a run-time exception. *** [Capture using try Catch]* * 1. Use try{} to capture potentially problematic code, using multiple catch blocks for different exception handling operations. * When an exception occurs in a try block, the corresponding catch block is entered and the remaining code in the try is no longer executed. ** !! After catching an exception with a try catch, the program's exception will not block the program's operation. * * 2.catch blocks can have more than one, generally end with exception, which means catch all exceptions; catch blocks must be small to large in order. * * 3. E.getmessage () get the error message; E.printstacktrace () Prints the wrong stack information * * 4.try_catch in order to ensure that the program is not interrupted, you must ensure that all exceptions in the program are captured. * * 5.finally{} Indicates whether the program has a statement that must be executed for an exception to occur. Typically used for operations such as closing a stream, releasing resources, and so on. Even if there are return;* in the block, you must execute the statement in finally. Use system.exit (int i); The program completely ends and does not perform finally. * * 6.try-finally can be combined to exist, instead of having to include a catch to indicate that an exception has not been processed, executing the statement in finally. Other code cannot be executed. ****** [Throws and throw]* * 1. In the method body, if an exception occurs, you can not make a try-catch capture, you can use throws to throw on the method declaration; * After it is thrown, it is captured by the place where the method is called, and if not captured, it can continue to be thrown. * In principle, exception handling must be done in the main method, which will cause the program to fail to discover if it continues to throw. * * 2.throws If you throw more than one exception to use, separate. * * 3.throw throws an exception manually in the program public static void Setage (int i) {if (i<0| | I&GT;100) {* Throw new IllegalArgumentException ("The age you entered is not legal"); * * 4. If you throw a check-type exception using throw, you must use throws on the method body to throw an exception, If you throw a run-time exception with throw, you do not need to throw an exception on the method body. * * 5 If the exception system does not throw new RuntimeException ("Exception"), or throw new* Exception ("Exception"). } }"Common exceptions"ArithmeticException-Arithmetic Operation exception numberformatexception-numeric format exception indexoutofboundsexception- Subscript out-of-bounds exception negativearraysizeexception-Creates an array with a negative size error exception NullPointerException-NULL pointer reference exception classcastexception-type cast exception.* [use of log4j]* * 1. Import Log4j.jar add** 2. Create log4j.propeities** 3 in the log4j sibling directory. In the class that needs to print the log, get a Log object by Logger.getlogger (the class name. Class). * * 4. Use the Log object to invoke different levels of print statements, respectively, for the output of the log. * Logr.debug ("Print a debug Message"), Logr.info ("Print a Info"), Logr.warn ("Print a warn message"), Logr.error ("Print an error message");  #1. Log4j.rootlogger = Total Configuration # log4j.rootlogger=[level],appendername1 (adapter name), Appendername2. [level]:error 3Warn 4INFO 6DEBUG 7#log4j.rootlogger= Setting info indicates that the level is greater than info can be log output. Log4j.rootlogger=debug, logfile,stdout  #2. Configure each adapter, # Log4j.appender. Adapter name = Specify the output destination for the log # Org.apache.log4j.ConsoleAppender (console), # Org.apache.log4j.FileAppender (file), # Org.apache.log4j.DailyRollingFileAppender (Generate a log file every day), # Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size), # Org.apache.log4j.WriterAppender (sends the log information in stream format to any specified place) #3. log4j.appender.stdout.target= set the console to print the statement style (only if the console has one)  # 4.log4j.appender.logfile.append = True true, does not overwrite, False writes when the file is emptied and then written.   #5. log4j.appender.logfile.Threshold = Debug Sets a separate level for the current adapter.   #6. log4j.appender.logfile.layout= which layout structure to use for log display# org.apache.log4j.HTMLLayout (layout in HTML table), # org.apache.log4j.PatternLayout (flexibility to specify layout mode), # Org.apache.log4j.SimpleLayout (contains the level and information string for log information), # Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.)   #7 of log generation. log4j.appender.logfile.layout.conversionpattern= sets the format of the patternlayout. #%M The message specified in the output code #%p output priority, that is, debug,info,warn,error,fatal#%r output from the application boot to output the log information in the number of milliseconds #%c output belongs to the class, usually the full name of the class #%t Output the thread name that generated the log event #%n output a carriage return newline, the Windows platform is "\ r \ n", the UNIX platform is "\ n" #%d output the date or time of the log point, the default format is ISO8601, you can specify the format later, such as:%d{yyy MMM DD HH:mm:ss, SSS}, output similar to: October 18, 2002 22:10:28, 921#%l The occurrence of the output log event, including the name of the class, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)   #8. LOG4J.APPENDER.LOGFILE.MAXFILESIZE=20MB You can set the maximum file size when Org.apache.log4j.RollingFileAppender.   #90log4j. appender.logfile.maxbackupindex=10 Org.apache.log4j.RollingFileAppender You can set the maximum number of files.

Getting Started with Java article nineth: Exceptions and log4j use

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.