Java-Exception Summary

Source: Internet
Author: User

Java-Exception Summary

definition of an exception
1, Exception: is the description of the problem, the object of the package

2, abnormal system:
Throwable
|–error
|–exception
|–runtimeexception

3, the characteristics of abnormal system:
All the classes in the anomaly system and the established objects have the ability to be parabolic, that is, the throw and throws keyword operations, only the exception system to have this feature

throw is defined inside the function to throw an exception object
throws is defined on a function , used to throw an exception class, can throw multiple exception classes, separated by commas

When the function content has throw throws the exception object, does not carry on the try processing, must be declared on the function, otherwise the compilation fails

Except RuntimeException, that is, if a RuntimeException exception is thrown inside the function, it is not declared on the function.

If the function declares an exception, the caller needs to handle it, and the method can be a throw can be a try

4, there are two types of exceptions:
The exception was detected at compile time :
The exception is compiled, if not processed (no throw, no try), compilation fails
The exception is marked, which means that it can be processed

Run-time exception (not detected at compile time):
At compile time, do not need to handle, the compiler does not check, once the program appears this kind of exception, the program ends directly

two finally
A finally defined code block that defines code that must be executed, typically to close a resource
even if there is a return statement in front, the finally statement is still executed,
Only System.exit (0) will not execute when the system exits

finally{    一定会执行的代码;}

There are three ways to combine:

try{}catch(){}
try{}finally{}
try{}catch(){}finally{}

Note:
The code that is typically defined in 1,finally is a resource that is closed because the open resource must be freed.
2,finally only one situation will not be performed when executed to System.exit (0)

Three custom exceptions:
Define class inheritance Exception or runtimeexception
1, in order for the custom class to have a parabolic
2, let this class have the common method of Operation anomaly

When you want to define the information for a custom exception, you can use a feature that is already defined by the parent class
The exception information is passed to the constructor of the parent class.

class MyException extends Exception{    MyException(String p_message)    {        super(p_message);     } }

Custom exception principle: to encapsulate the problems in the program according to the idea of Java facing objects

four advantages of the exception:
1. Encapsulate the problem.
2, separation of normal process code and problem-handling code to facilitate reading

Five exception handling principles:
1, there are two ways of handling: try or throw

2, call to throw out the function of the exception, throw a few, just deal with a few,
A try can correspond to multiple catch

3, multiple catch, the parent's catch is placed at the bottom

4,catch, you need to define a specific processing method, do not simply define the output statement of Printstacktrace
Can not write, when the catch to the exception, this function can not handle the time, you may continue to throw in the catch

try{    thrownew AException();}catch (AException e){    throw e;}

If the exception is not handled, the soldier does not belong to the exception of this function, you can convert the exception after the
Throws the exception associated with the function

Or an exception can be handled when it is necessary to provide an exception-related problem with this function,
Allows the caller to know, and handle, to convert a new exception after the catch exception is processed.

try{    thrownew AException();}catch (AException e){    //对AException 处理    thrownew BException();}

You can refer to the Exception.java class file

Six Catch
Catch is used to handle exceptions, and if no catch represents an exception that has not been processed
If the exception is a detection-time exception, you must declare

Seven exception coverage
1, when the subclass overrides the parent class, if the parent class's method throws an exception, then the child class overrides the method, can only throw the parent class exception or changes the exception subclass

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

3, if there is no exception thrown in the method of the parent class or interface, then the subclass cannot throw an exception when overriding the method.

4, if the subclass method has an exception, it must be done with a try and cannot be thrown

Copyright NOTICE: Welcome to communicate the error of the article, must humbly accept, QQ872785786

Java-Exception Summary

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.