Exception Try catch in Java

Source: Internet
Author: User
Tags finally block getmessage terminates try catch log4j

1. What are the reasons for learning anomalies?

If there is no exception handling mechanism, then a small problem with the program will result in "program termination Run". The actual development is obviously impossible, so the exception is very important for the program.

2. How to handle Exceptions:
A.If structure:

1. Code size is large, code structure is not obvious

2. Most of the programmer's energy is not used in the process, but "plugging the loopholes."

3. Left a loophole, the program still terminates.

B. Exception handling mechanism:
Try Catch finally throw throws

Exception catch throws exception declaration exception.


3. Structure of exception handling:

The first type: try---catch:

The program runs normally: only the code in the try code block is taken;


Program exception: Go to the code before the try error--will produce an exception object;

The exception object is compared to the type of exception caught by the catch block;

If matching: Executes the corresponding catch block code;

If it does not match: The program still terminates.


The second type: Try--catch---catch:

The program runs normally: only the code in the try code block is taken;


Program exception: Go to the code before the try error--will produce an exception object;

The exception object is compared to the type of exception caught by the catch block;

If matching: Executes the corresponding catch block code; "Only one matching catch block code"

If it does not match: The program still terminates.

Note: 1. The exception type of the parent class "must" be in the last catch.
2. The subclass's exception type Order is independent.
3. The last catch block is the exception type that holds the parent class.


The third type: try--catch--catch--finally


The program runs normally: only the code in the try code block; The catch block does not execute; Go finally.


Program exception: Go to try to error before the code-"will produce an exception object;"

The exception object is compared to the type of exception caught by the catch block;

If matching: Executes the corresponding catch block code; "Only one matching catch block code"
---, finally.

If it does not match: The program still terminates. ---, finally.


"Question: Under what circumstances, finally do not go"

1.return:finally code blocks are still executed;
Executes the code before the return in the catch block;
Then execute the code in the finally block;
Finally executes the return.

2.system.exit (1); program exits. Terminates the execution of a finally block.

4. Output exception information in the form of:
SYSTEM.OUT.PRINTLN ("hint message");
System.out.println (E.getmessage ());
E.printstacktrace ();

5.throws keywords (declaring exceptions):
---role: I can not handle, you to deal with, I tell you should pay attention to matters.


When you need to tell the caller that the method needs to be aware of the exception, use throws.


Use:

1. At the time of the method definition:

public void Method Name 1 () throws Exception type 1, exception type 2{

}

2. Method invocation: Two methods to handle:

A. Using Try--catch for exception trapping

try {
Called Method name 1 ();
} catch (Exception type 1 e) {
E.printstacktrace ();
} catch (Exception type 2 e) {
E.printstacktrace ();
}finally{

}

B. Do not process, continue to declare exceptions:

public void Method Name 2 () throws Exception type 1, exception type 2{
Called Method name 1 ();
}

6.throw (Throws an exception):

The---is not an exception, and for the caller's attention, the exception is thrown manually.

Use throw:
public void Setsex (String str) {
if (!str.equals ("male")) {

throw new ClassCastException ("Gender must be male or female");

}else{

String Sex=str;
}
}
7. Exception Classification:

Object

Throwable

Error Exception

Checked (Compile exception) Running (Run exception)

Error: A critical error that cannot be recovered by the program itself.

exception:→ a non-critical error thrown and handled by a Java application.

Checked: Exception must be handled. (not processed, cannot be run)

Running: Does not handle can, once an exception occurs, the program terminates.

8.log4j Open Source Log file:

Function: The exception information is recorded in a separate file or output to the console.

Steps:
1. Put the Log4j-1.2.17.jar method project under the Lib folder; Load: Select--Right----buil path---add to buil path

2. Put the log4j.properties in the project SRC, cannot be placed outside the SRC, or the SRC subdirectory does not work.
3. Call:

To create a Logger object:
public static Logger Logger=logger.getlogger (Test.class);
Write the exception statement in the CATCH block:
Logger.debug ("hint message");
Logger.debug (E.getmessage ());
|
This can be a debug,info,warm,error,fatal five output levels

4. Refresh the project, the Jbit.log file appears, log the exception information

Exception Try catch in Java

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.