Main content: * Java exception concept. * Classification of Java exceptions. * Exception capture and handling. How does the q:java handle the exception? a:java defines a number of exception classes that correspond to a wide variety of possible exception events, which, if an exception event occurs during the execution of a Java program, generates an exception object and submits it to the Java runtime system, which encapsulates the corresponding information for the exception event when When an exception object is received by the Java runtime system, it looks for code that can handle the exception and handles the current exception object. exception (Exception): * refers to the run-time error, that is, when your program starts executing, the execution period of the error, rather than when we knocked Javac this time. * It is most important to observe the wrong name and line number!!! And to dare to debug!!! Remember a word: The program is the tune out!!! Exception Concepts: * Java Exceptions are a mechanism provided by Java for handling errors in a program. * The so-called error refers to some unusual events that occur during the running of the program (for example, except 0 overflow, array subscript out of bounds, the file to be read does not exist). * Well-designed programs should provide a way to handle these errors when they occur, so that the program does not block or produce unpredictable results because of an exception. * When an exception event occurs during the execution of a Java program, an exception object is generated that encapsulates the information of the exception event and will be presented to the Java Runtime system, a process called throw (throw) exception. * When the Java runtime system receives an exception object, it looks for code that can handle the exception and handles the current exception object, a process known as catch (Catch) exception. Classification of exceptions: * J2SDK defines a number of exception classes that correspond to a variety of possible exception events. * The root class of the anomaly is throwable. * Throwable It has two known sub-classes: Error, Exception. * There are a lot of exceptions under Exception, there is a special exception called: Runtime exception (runtimeexception) * level, following a brief figure: throwable / \ error Exception &N Bsp / / \ &NBSP, ...  , .... RuntimeException &NB Sp , &NB Sp \ &N Bsp ... * the difference between these anomalies: * * Throwable: Called can be thrown, any event, as long as you inherit from Throwable here, you can use th The rows statement throws it out. * * * * ERROR: Called system errors, generated and thrown by the Java virtual machine, including dynamic link failure, virtual machine error, etc., the program does not handle it, it is said that the Java Virtual machine error, your program can not handle, you do not have to deal with. * * * * Exception: The base class of all exception classes, whose subclasses correspond to a variety of possible exception events, generally require the user to explicitly declare or capture, but also say that your program can handle errors, you can catch (catch) it. **** RuntimeException: A special kind of exception, such as dividing by0, array subscript out of bounds, and its generation more frequent, processing trouble, if the explicit declaration or capture will have a significant impact on program readability and operational efficiency, so the system automatically detects and gives them to the default exception handlers (users do not have to handle them); it is often a mistake that you can capture it , or you can not capture it (we usually do not capture), do not capture the same can run, but the run-time error, because out of the frequent, you old capture it will be exhausted, a metaphor, example: you drive in the road, every see a small stone you get off to pick up, then you do not tired to die, so it is better to **** Common Exception: called Must catch (catch) Exception, requires you have to catch it to go forward, usually see the method behind the throws, OK, this class of exceptions you have to catch or declare throw! * * * To illustrate the difference between Error and Exception, example: You drive on the mountain road, your car burst, then you can stop to repair (change the tyre), this is called Exception, because you can handle it, then if the mountain collapsed, you can handle it? You can't, this is called Error, you can't handle it. exception capture and handling: * Java's exception handling mechanism allows exception events to be searched forward in the order in which they are called until an exception handler is found that matches the exception type. * Try statement * * try {...} statement specifies a piece of code that is the scope of capturing and handling exceptions at once. * * During execution, the code may produce and throw one or more types of exception objects, and the catch statements behind it will handle the exceptions accordingly. * * If no exception is generated, all the catch code snippets are skipped and not executed. * Catch Statement * * In a catch statement block is the code that handles the exception, and each try statement block can accompany one or more catch statements to handle the different types of exception objects that might be produced. * * Exception objects declared in catch (catch (Someexception e)) encapsulate information about the occurrence of an exception event, and some methods of this object can be used in a catch statement block to obtain this information. * * For example: *** getMessage () method, used to get information about the exception event. * * * PrintstacktraThe CE () method is used to track the contents of the execution stack when an exception event occurs. * FINALLY Statement * * The finally statement provides a unified exit for exception handling, allowing for uniform management of the state of the program before the control process is transferred to other parts of the program. * * The code specified by finally is executed regardless of whether the block specified in the try is thrown with an exception. * * Usually in a finally statement, you can do cleanup of resources, such as: *** close open files. * * * Delete temporary files. * * * ... Custom exception: * using a custom exception generally has the following steps: 1, declaring its own exception class by inheriting the Java.lang.Exception class. (It is also possible to inherit other exception classes, but it is not recommended that if you inherit the Java.lang.RuntimeException exception class, it means that exceptions thrown from your custom exception class are also captured and not captured!) 2, generate an instance of the custom exception in the appropriate location of the method, and throw it with a throw statement. 3. In the declaration section of the method, declare the exception that the method might throw with the throws statement. * Example Program: public class MyException extends Exception { private int id; myexception (String _message, int _id ) { Super (_message); this.id = _id; } public int getId () { return th Is.id; } public static void Main (string[] args) throws exception{ try { if (args.length! = 2) throw new MyException ("username or UserPassword is null!", 001); if ("null". equALS (Args[0])) throw new MyException1 ("username is null!", 002); if ("". Equals (Args[1])) throw new MyException ("UserPassword is null!", 003); system.out.println ("Honorable Gold VIP:" + args[0] + ", welcome you! "); } catch (MyException me) { me.printstacktrace (); //system.out.println (" Error "); } }} Note: The overriding method needs to throw an exception that is identical to the exception type thrown by the original method, or does not throw an exception, all other conditions are not good, throw the exception range is not big, not small, The number is not the same. Summary: * a graph. * Five keywords. * Catch the small first, then catch the big. * Exception and rewrite relationships.
Java-Exception handling