Summary of basic knowledge of exception

Source: Internet
Author: User

1. What is an exception?

is a description of the problem. To encapsulate the object in question.

2. Abnormal system:

Throwable
--error
--exception
--runtimeexception
  2.1 Features of the anomaly system:

All classes in the exception system and the objects created are parabolic, which means they can be manipulated by the throw and throws keywords.

Usage of 3.throw and throws:
A throw is defined within a function and is used to throw an exception object;
Throws is defined on a function, used to throw an exception class, and can be thrown multiple separated by commas.
The throws throws an exception object inside the function, does not perform a try processing, must be declared on the function, or the compilation fails.
Note that, except for runtimeexception, the function may not be declared if the RuntimeException exception is thrown inside the function.
If the function declares an exception, the caller needs to handle it, and the processing method can throws the try.

4. There are two types of exceptions:
Exception detected at compile time
The exception is self-esteem when compiling, if there is no processing (no throws and no try, the compilation fails.) )
Run-time exception (not detected at compile time)
At compile time, no processing is required and the compiler does not detect.
This exception occurs, it is recommended not to process, let the program stop, the code needs to be corrected.

Processing statement:
Try
{
     required instrumented code
}
Catch
{
    handle exception code
}
Finally
{
     code statements that are bound to execute
}
5. Exception three formats:
First:
try{}
Catch {}
Second:
try{}
Catch {}
finally{}
Third type:
try{}
Final ly{//OFF resource}
Note:
1.finally is typically defined as closing the self-source code. Because the resource must be freed
2.finally there is only one case where the system does not execute. Exit (0); finally does not execute.

6. Custom Exceptions:
Inherit exception or inherit RuntimeException
1. In order to make the custom class can be parabolic.
2. Let the class have a common approach to operating exceptions.
When you want to customize the information for an exception, you can use features that are already defined by the parent class.
The exception information is passed to the constructor of the parent class.
Class MyException extends Exception
{
MyException (String message)
{
Super (message);
}
}
Custom exceptions encapsulate specific issues that arise in your program in the light of Java's object-oriented thinking.

7. Benefits of Exception:
1. Tell the problem to encapsulate
2. Separate the normal process code from the problem code for easy reading.

8. Exception Handling principles:
1. There are two ways of handling: try or throws.
2. When calling to the function that throws the exception, throw more than one and handle several
One try corresponds to multiple catch
3. Multiple catch, the parent class's catch is placed at the bottom.
Within 4.catch, it is necessary to define a specific processing method, which does not require a simple definition of printstacktrace or output statements. Don't even write it.
When the exception is caught, the feature cannot be processed and can continue to be thrown in the catch.
Try
{
throw new Aexception ();
}
catch (Aexception e)
{
Throw e;
}

If the exception is not handled, it does not belong to the exception that is present with the feature.
You can convert an exception and then throw an exception that is related to that feature.
Or an exception can be handled when it is necessary to provide the problem related to the function of the exception, let the caller know, and processing.
Try
{
throw new Aexception ();
}
catch (Aexception e)
{
For aexception processing,
throw new Bexception;
}
For example: Remittance.

9. Exception Precautions:
When the child parent class is overwritten;
1. The exception that the subclass throws must be a subclass or a subset of the parent class.
2. If the parent class or interface does not throw an exception, the subclass overrides an exception, and only the try cannot be thrown.

Summary of basic knowledge of 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.