Java Basic Learning Supplement--Exception handling and log4j logs

Source: Internet
Author: User
Tags finally block getmessage throw exception throwable log4j

Exception Handling in Java

Exceptions are errors in the program, but not all errors are exceptions, and errors can sometimes be avoided.

The base class throwable:throwable for all exceptions in Java is also divided into the error class and the exception class.

Error: An instance of the error class and his subclass represents the JVM's own error. Errors cannot be handled by programmers through code, and error rarely occurs.

Exceptions: Exception and his subclasses represent a variety of unexpected events that are sent when the program is run. Can be used by the Java exception handling mechanism. Exception is also divided into check-time exceptions and run-time exceptions (RuntimeException)

Check exception: The most representative of the inspection exception is the user error or the exception caused by the problem, which is not foreseen by the programmer.

Run-time Exceptions: Runtime Exceptions are exceptions that can be avoided by programmers. Contrary to the inspection exception, run-time exceptions can be ignored at compile time.

Exceptions in Java are run-time exceptions and check-time exceptions, run-time exceptions represent RuntimeException, and subclasses that do not need to be captured in the program, and most can be controlled by code to avoid When checking for exception packets, the runtime exception represents runtimeexception and all exceptions other than subclasses that require capture processing in the program.

Exception capture

1. Use: Use the Try/catch keyword to catch exceptions. The Try/catch code block is placed where the exception can occur. Use a try to catch the code that could have an exception, use a catch block for different exception operations, and when the program in the try block goes into an exception, it goes to the corresponding catch block and no longer executes the remaining code in the try block.

After catching an exception with Try/catch, the program will not be blocked if an exception occurs.

2. Wording:

Try {   //  Possible exception code block }catch(ExceptionName1 E1) {   // Catch Block } Catch (ExceptionName1 E2) {//catch block }

Note: A try code block follows multiple catch code blocks, typically ending with exception, which means that all exceptions are caught, but if multiple catches are used, the exception exception cannot be placed before the specific exception to be captured.

3. Common method: E1.printstacktrace (): print stack information; E1.getmessage (): Get error message

Try/catch If you need to ensure that the program is not interrupted, you must ensure that the catch is able to catch any exceptions that may occur in the program

Finally: Indicates that no matter whether the program has an exception, even if a return statement appears in the try, you must finish finally to end the current method.

Finally block regardless of whether the exception occurs, it must also execute as long as the corresponding try executes. There is only one way to keep the finally block from executing: system.exit (). So the finally block is typically used for resource release operations such as closing the I/O stream, shutting down the database connection, and so on.

Try {  //  program code }catch(Exception type 1 exception variable name 1) {  //  program code } Catch (Exception type 2 exception variable name 2  ) {//  program code }finally{  // Program Code }

Note: A try must have at least one catch block, otherwise there must be at least 1 finally blocks to form a statement that catches the exception.

Throw exception

If a method has an exception and is not caught (an inspection exception), then the method must be declared with the throws keyword, throw the exception, throw it out, catch the exception by calling the method, or continue to throw if you do not want the capture to occur. In principle, if an exception occurs in the main method, it is not recommended to throw it, and if thrown, it may cause the program to fail to function properly.
The throws keyword is placed at the end of the method name, and if multiple exceptions are thrown, the exceptions are separated by commas.

You can also use the Throw keyword to throw an exception, that is, to manually throw an exception in the program, whether it is newly instantiated or just captured.

If you throw a check-type exception using throw, you must use throws to throw the declaration, and if you throw a run-time exception using throw, you do not have to use throws to throw the declaration.

Custom exceptions

Custom exception classes, you must base existing exception classes
Typically, exception or runtimeexception are inherited, respectively, to declare a check-time exception and a run-time exception.
and rewrite its getmessage () method.

Use of log4j logs

Use of log4j:
1. Import Log4j.jar
2. Create the Log4j.propetites profile under SRC peer
3. Get a Log object through Logger.getlogger () in the class that needs to print the log
4, respectively call different bureau level of the print statement, the output of the log

Settings for the log4j configuration file: http://www.blogjava.net/zJun/archive/2006/06/28/55511.html

1  Public classdemo03_log4j {2 3     /**4 * Get the Log object via Logger.getlogger, and pass the parameter into class5      * 6 * Use of log4j:7 * 1, import Log4j.jar8 * 2, in the SRC directory (with the package sibling) to create the Log4j.propetites configuration file9 * 3, in the class that needs to print the log through Logger.getlogger () to get a log objectTen * 4, call different bureau level of the print statements, the output of the log One      */ A     StaticLogger log = Logger.getlogger (demo03_log4j.class); -      -      Public Static voidMain (string[] args) { the         //Print an error message -Log.error ("Print an error message"); -         //Print a warning message -Log.warn ("Print a warning message"); +         //print an info message -Log.info ("Print an info"); +         //print a test message ALog.debug ("Print a test message"); at     } -}

Java Basic Learning Supplement--Exception handling and log4j logs

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.