Description: 1. five keywords: 1.try: try to catch exceptions. The specific description is as follows 2. catch: When an exception is caught by try, the system automatically generates an exception message. The exception information is sent to catch, indicating that I have caught the exception. 3. throw: throw an exception actively. I found an error that I could not handle and could cause my program to run abnormally. So I threw the exception. (To whom? This depends on the situation.) Situation 2: I found that an incorrect part will affect my future program running. To avoid larger errors, I first propose (throw) this "problem" 4. throws: A method declaration throws such an exception for custom exceptions. (This can be temporarily bypassed by beginners.) 5. finally: A code block that must be executed by the program regardless of whether an exception occurs. For example, a typical example of disabling a database connection is as follows. Mostly used for cleaning. Finally seems "authoritative" (must be executed), but System. exit (1); will make it lose this authority. 2. try + catch 1. Joint attack to ensure normal operation of the program. 2. understanding: We want to write a program like this: if (the program is normal) {my operations} else {processing is not normal}. However, this program is too general to define or enumerate it, so we use try to monitor whether our operations are normal. when the operation is abnormal, we can handle the abnormal situation. Try-catch is doing such a meaningful thing. 3. note (newbie can bypass it first): 1. there can be multiple catch methods. Whoever can catch them. If they cannot catch them, they will let others catch them. However, this requires a level (catch in a small range) 2. variables in the try block can only be used in try. 3. java 7 provides a method to catch multiple exceptions at a time. Each exception is used | split (note that it can only be used in JDK 7. the AutoCloseable interface under java 7 can simplify the program. If you are interested, you can Baidu or view java api 4. several methods of Exception (see the example) 5. custom exception: 1. as mentioned above, you are not allowed to enumerate all exceptions. Even if you have enumerated all exceptions, you cannot break through cognitive differences. (In the world of elephants, humans are heterogeneous.) 2. define an exception class with its own business logic. implementation Method: Inherit from Exception or its subclass [java] package com. cxy. exception; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. util. list;/*** @ author cxy */public class ExceptionTest {public static void main (String [] args) {try {List l = null; l. get (0);} catch (NullPointerException | ArrayIndexOutOfBoundsException e) // Note: Only Java 7 can be used here. If you are not Java 7, remove one Exception {System. out. println ("I caught an exception"); System. out. println ("exception information:" + e. getMessage (); e. printStackTrace (); // prints the trace stack information for exceptions} System. out. println ("==================");/* throw * purpose: * 1. throw an exception that I cannot handle * 2. discover problems in advance to avoid larger problems (in practice) * when a person wants to build a villa and finds that he is located in the middle of the Pacific Ocean, I throw this problem to avoid the sand and gravel behind it, the Engineering Design is well done, and there is no way to build it. * Effect: cause the program or method to terminate (if the Exception is not caught) **/try {throw new Exception ("Nothing left to play "); // actively throw an Exception} catch (Exception e) {e. printStackTrace ();} System. out. println ("============================= "); // throws usage try {test ();} catch (NullPointerException e) {System. out. println ("catch the exception thrown by the test method");} System. out. println ("============================="); // use FileInputStream in finally = null; // This is an input stream-related knowledge. Please refer to the subsequent article try {/* The file cxy.txt error is not found. * There is a problem here. whether an exception occurs or not, we need to disable the system. * Before Java 7, we use finally to deal with such problems, operations to be performed regardless of whether an exception occurs * Java 7, the AutoCloseable interface does not need to use finally to handle this problem (if there is time, I will write a related article) */FCM = new FileInputStream ("cxy.txt");} catch (FileNotFoundException e1) {e1.printStackTrace ();} finally {try {FCM. close ();} catch (Exception e) {// including a try here, because the program is afraid of encountering a null or I/O Exception e. printStackTrace () ;}} System. out. print Ln ("======================= "); // custom exception class try {throw new MyException ("Chinese exception information");} catch (MyException e) {e. printChineseMsg ();} System. out. println ("although there are many exceptions, I am finished normally ~ ");}/** Declare in advance what kind of exception this method may throw, ask the caller to handle */public static void test () throws NullPointerException {throw new NullPointerException (); // throw a null Exception} // class MyException extends Exception {public MyException () {} public MyException (String msg) {super (msg ); // Of course, here you can add some log operations} www.2cto.com/*** Chinese exception information we expect to understand */public void printChineseMsg () {System. out. println ("exception information:" + this. getMessage ());}}