Javase (6) _ exception

Source: Internet
Author: User

First, the concept of abnormal

1. Java exceptions are a mechanism provided by Java for handling errors in a program.

2, the so-called error is the program in the course of the operation of some unusual events (such as: In addition to 0, array subscript out of bounds, the file does not exist, etc.).

3, in the execution of Java program, such as an exception event, it can be encapsulated as an exception object, the exception object encapsulates the information of the exception, and to the Java Runtime system, the process is called throwing an exception (throwing away is the exception object).

4. When the Java runtime system receives an exception object, it looks for the code that can handle the exception and gives the current object its processing, a process known as catching an exception.

Ii. Classification of anomalies

1. Error,throwable subclass, generated and thrown by a Java virtual machine, is used to represent a serious problem that a reasonable application should not attempt to capture, for example: Outofmemoryerror,noclassdeffounderror, The end of error is wrong, where the parent name is a subclass suffix, can be used for reference.

2, Exception,throwable subclass, the parent class of all exception classes, its subclasses have a variety of possible exception events, generally require the user to display the declaration or capture.

3, runtime exception,exception subclass, Java virtual machine normal running during the exception thrown by the parent class, such as except 0, subscript out of bounds, and so on, it produces more frequent, handling trouble, if the display of the declaration or capture will have a significant impact on the readability and operational efficiency of the program, so the system automatically detects and gives it to the default exception handler, which the user does not have to handle, that is, the program stops directly.

Third, abnormal system

Exception classes and exception objects can be thrown, they all have a parabolic, is the unique characteristics of the throwable system, the Throwable class is the Java language of all errors or exceptions of the superclass, only the class and objects in the system can be thrown through the Java virtual machine or Java throw statement, Similarly, only one of this class or its subclasses can be a parameter type in a catch clause.

三、五个 keywords

Try,catch,finally,throw,throws,throws is used on the function, throws the exception class, can have several, the bean number splits, THORW defines in the function, followed is the exception object, some methods (or functions, for example IO some methods) through the throws Excepiont, after loading the method declaration throws an exception (improve security, let the caller handle), we call this method must handle or continue to throw to call, or compile error, here also embodies the exception of the two processing mode of a processing, a continuation of the throw.

Third, exception handling

1. Single Exception handling

try{//code that needs to be instrumented, after throwing an exception, the following statement in the Try section no longer executes}catch (//Exception class variable) {No exception catch will be ignored, main 2 method, GetMessage (), Printstacktrace () ( This is the JVM default call method)//Handle exception code, catch does not recommend not processing or directly print the stack or sysout, you can log the exception logging file}finally{//must be executed statements, such as database connection whether the report is not reported error after the execution of all to close}

2. Multiple exception handling

1, it is recommended to declare specific anomalies, so that can be targeted treatment.

2, multiple exceptions multiple catch, parent catch is placed below, otherwise do not execute, compile error, equivalent to write code under the return statement

3, there is an exception in the function is not processed, the function is ended, so multiple exceptions will not occur at the same time, you can use polymorphism to pass a exception class, but no targeted processing

4, the catch does not write things, the equivalent of hiding the problem, the program continues to execute down.

Iv. Custom Exceptions

1, since Java can be exceptions to the exception object encapsulation, we can also be specific to the exception of the object encapsulation, which is the custom exception, for example, we can specify that the divisor cannot be less than 0, otherwise throw an exception.

2, custom exception Java does not know, can only manually throw (throw), non-custom Java recognition will automatically throw, we can also manually throw Java exception.

3. There are several steps to using a custom exception:

1. Declare your own exception class by inheriting the Java.lang.Excepion class.

2. Generate the instance of the custom exception in the appropriate location and throw it with the throw statement.

3. In the declaration section of the method, declare the exception that the method may throw by using the throws statement.

Cases:

 Public class extends exception{    privateint  ID;      Public MyException (String message,int  ID) {        Super(message);         this. id=ID;    }      Public int getId () {        return  ID;    }}
 Public classTest { Public voidRegist (intNumthrowsmyexception{if(num<0) {Throw NewMyException ("The number is negative, unreasonable", num); } System.out.println ("Number of Registrants:" +num); }     Public voidManager () {Try{regist (100); }Catch(MyException e) {System.out.println ("Registration failed with Error ID:" +e.getid ());//log can be written hereE.printstacktrace (); } System.out.println ("End of Operation"); }     Public Static voidMain (string[] args) {Test T=NewTest ();    T.manager (); }}

PS: abnormal misunderstanding and experience

http://www.ibm.com/developerworks/cn/java/j-lo-exception-misdirection/

Javase (6) _ exception

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.