Dark Horse Programmer-object-oriented exception

Source: Internet
Author: User
Tags try catch

--java Training, Android training, iOS training,. NET training, look forward to communicating with you! ——-

1. Abnormal

Exception: The program is not normal at run time, is a description of the problem, the problem is encapsulated object.
Origin: The object that Java describes to the abnormal situation.

    1. For serious problems, Java is described by the error class.

    2. For non-critical problems, Java is described by the exception class.

▲error and Exception (runtimeexception) have some common content.
All classes and objects created in the anomaly system are parabolic and can be thrown with the throws keyword operation.

2. Exception handling

try{    需要被检测的代码;}catch(异常类,变量){     处理方式;}finally{    一定会执行的语句//System.exit(0);系统退出,jvm结束}

3. Common method operation for the caught exception object

    • GetMessage () exception information
    • ToString () exception name, exception information
    • Printstacktrace () exception name, exception information, where the exception occurred

4, throws Exception

There may be a problem with the functionality of declaring this feature through the throws keyword.
Declare an exception on the function, let the caller handle it, and the compilation fails without processing.

5, the treatment of multiple anomalies

    1. When declaring an exception, it is recommended to declare a more specific exception so that the processing can be more specific.
    2. Declaring several exceptions, there are several catch, and if more than one catch block has an inheritance relationship, the parent catch block is decentralized.

6. Custom Exceptions

According to the Java object-oriented idea, the specific problems appearing in the program are encapsulated.

When a throw throws an exception object inside the function, the corresponding processing action must be given:

    1. In internal try catch processing.

    2. Declaring on a function lets the caller handle it.

▲ Custom exceptions must be custom class inheritance exception.

▲ the reason for inheriting exception.

Exception classes and exception objects are thrown, they all have a parabolic, the throwable is the unique characteristics of the system, only the classes and objects in this system can be throw and throws operation.

Example: Calculation program, divisor is0and1is unable to calculate, throws an exception class fushuexception extends Exception{    Private intValue Fushuexception () {Super(); } fushuexception (String msg,intValue) {Super(msg);//The exception information is super to the parent class. } Public intGetValue () {returnValue }} class Demo{    int Div(intAintb) throws Fushuexception {if(b<0)Throw NewFushuexception ("negative number appears", b);returnA/b; }} class exceptiondemo{     Public Static voidMain (string[] args) {Demo d =NewDemo ();Try{intx = d.Div(4,-9); System.out.println ("x="+X); }Catch(Fushuexception e) {System.out.println ("The negative number of the error is"+e.getvalue ()); }    }}

7, the difference between throw and throws

    • Throws is used on functions, followed by exception classes, which can be separated by multiple commas.

    • Throw is used within a function, followed by an exception object.

8. Runtime

Runtime and its subclasses are thrown inside a function without declaring it, and if the exception is declared on the function, the caller does not have to process it.

9, the embodiment of the exception in the child parent category coverage

    1. When a subclass overrides a parent class, if the method of the parent class throws an exception, then the overriding method of the child class can only throw the exception of the parent class or the subclass of the exception.

    2. If the parent method throws more than one exception, the child class can only throw a subset of the parent exception when overriding the method.

    3. If no exception is thrown in the method of the parent class or interface, the subclass can not throw an exception when overwriting the method.

10. There are two kinds of anomalies

    1. The exception is detected at compile time: The exception is at compile time if it is not processed (thrown. Try Catch), the compilation fails, the exception is identified, and the delegate can be processed.

    2. Runtime exceptions (not detected at compile time), do not need to be processed at compile time, this exception occurs, does not process, the program stops, need to correct the code.

Dark Horse Programmer-object-oriented 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.