Java Object-oriented sixth chapter

Source: Internet
Author: User
Tags getmessage log4j

First, abnormal

1. What is an exception?

Exceptions to Java programs that occur during operation

2, how to do exception handling in Java?

exception handling Mechanisms in Java

Try, catch, finally, throw, throws

try{

Code with the possibility of an exception

}catch (Exception type) {

Capturing and handling exceptions that occur

Return

System.exit (1);//finally statement block will not execute

}finally{

Code to execute regardless of whether the program has an exception

Try...catch...finally altogether there are 3 combinations of methods

Try...catch ... (Catch can be many, but be aware of the order of subclasses stepfather classes first)

Try...catch...finally ...

Try...finally ....

 Public classTest3 {Private StaticLogger Logger=logger.getlogger (Test3.class. GetName ());  Public Static voidmain (String [] args) {Scanner input=NewScanner (System.inch); System. out. Print ("Please enter dividend:"); intnum1=Input.nextint (); System. out. println (""); System. out. Print ("Please enter a divisor:"); intNum2=Input.nextint (); Try{System. out. println (num1/num2); System. out. println ("Thanks for using this program! "); }Catch(inputmismatchexception e) {logger.error ("An error has occurred! Divisor and dividend must be integers! ", E); }Catch(ArithmeticException e) {logger.error (E.getmessage ()); }Catch(Exception e) {logger.error (E.getmessage ()); }finally{System. out. println ("Happy Drink You use this program! "); } }} example
Example

3. What are the common types of exceptions? Table 6-1

4. Throw and THROWS2 keywords are all used to throw an exception

The difference has 3 points, P149 page

Different roles

Different locations

Content is different,

Ii. Log4j:1 Open Source Logging tool

Steps to use:

1. Add the log4j. jar file to the project

2. Create log4j.propterties file "Save log information Related Settings"

3. writing configuration information for logs "output level, destination, format"

4, in the program, using log4j logging (SQL log, Exception Log, business log and other 3 types of log information)

Java Object-oriented sixth chapter

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.