Detailed examples of Java exceptions

Detailed examples of Java exceptions

1. Definition of exception: abnormal conditions occur when the program is running.

Division of exceptions:

Error: a serious problem. Generally, no specific code is written to deal with the error.

Exception: a non serious problem. Exceptions can be handled in a targeted way.

2. Exception handling: (fixed format)

Handling of multiple exceptions:

1. When declaring exceptions, it is recommended to declare more specific exceptions so that they can be handled more specifically.

2. If the other party declares several exceptions, it corresponds to the qualified catch block. Do not define redundant catch blocks. If the exceptions in multiple catch blocks have inheritance relationship, the parent exception catch block is placed at the bottom.

*During catch processing, specific processing methods must be defined in catch.

*Catch is used to handle exceptions. If there is no catch, it means that the exception has not been handled. If the exception is an exception during detection, it must be declared.

Runtime exception: (i.e. runtime exception)

Is a special subclass exception in exception.

If the exception is thrown in the function, the function can pass the compilation without declaration; If the exception is declared on the function, the caller can pass the compilation without handling it.

It is used to declare on a function because it does not need to be handled by the caller.

The embodiment of exceptions in the coverage of child and parent classes;

1. When a subclass overrides the parent class, if the method of the parent class throws an exception, the overriding method of the subclass can only throw the exception of the parent class or the subclass of the exception. 2. If the parent method throws multiple exceptions, the subclass can only throw a subset of the parent exception when overriding the method. 3. If there is no exception thrown in the method of the parent class or interface, the subclass cannot throw an exception when overriding the method.

If an exception occurs in a subclass method. You have to try. Never throw.

Differences between throws and throw:

Throws: used on functions (between braces and braces), followed by exception classes, which can be followed by more than one. The middle is separated by "," (comma).

Throw: used in a function, followed by an exception object. You cannot add a statement after throw because the statement after throw cannot be executed.

Exercise:

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>