Dark Horse programmer----Java Base---exception, with related questions

Source: Internet
Author: User

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

: Exception (understanding)

(1) Abnormal situation of the program.
(2) Classification system of anomalies
Throwable
|--error Serious problem, we do not handle, such as memory overflow.
|--exception
|--runtimeexceptionRun-time exception, because our code is not rigorous, we need to fix the code
|--non-RuntimeException compile-time exception, must be handled, or program compilation does not pass
Add: If the code has an exception at run time, we'll look for the exception type in the API.
If the parent class of the exception is runtimeexception, the run-time exception
The parent class is not runtimeexception, which is the compiler exception.

(3) Exception handling (two options):
Default handling of A:JVM
The name, reason, location and other information of the exception are output in the console, but the program is also terminated.
B: Handle It Yourself
A:try...catch...finally
Write your own processing code, the following program can continue to execute
B:throws
You can't handle it, declare it on the method, tell the caller, there's a problem.
Format: Throws Exception class name
Note: This Gus must be followed by the method (), for example:
public static void Method () throws parseexception{}
Note: Try not to throw an exception on the main method, but it is possible to do so in order to lecture.
(4): Processing format for try...catch...finally
try{
Code that may be having problems
}catch () {
Describe or deal with a problem
}finally{
Freeing resources
}
Note: The less code inside the 1,try, the better, so you can save resources
There must be content in the 2,catch, even given a simple hint.
(4) Face test
A: What is the difference between a compile-time exception and a run-time exception?
Compile-time exception must be handled, otherwise compilation does not pass
Run-time exceptions can be handled without processing or
B:throw and throws, that's the difference.
Throw
In the method body, followed by the exception object name, and can only be a
Throw throws an exception object that executes the throw description there must be an exception generated here.
Throws
On the method declaration, followed by the class name of the exception, you can follow a number of exception class names, separated by commas.
Represents an exception that is thrown and is handled by the caller of the method.
Throws is a method of declaring an exception, which is a possibility, and this exception does not necessarily produce
(5) Finally keyword and its face question
A: Function: Finally to release resources,
Features: Its code is always executed.
Special case: The JVM exited before executing to finally
B: Face question
The difference between 1:final,finally,finalize?
Final: The ultimate meaning, can be modified class, class cannot be inherited, modified variable, variable is constant, modification method, method cannot be overridden
Finally: is part of exception handling for freeing resources. In general, the code will certainly execute, but if the JVM exits before the finally execution, it cannot execute the
Finalize: Is a method of the object class for garbage collection (this method is generally not used when programming)
2: If there is a return in the catch, will the finally be executed? If executed, before or after return
A: It executes and executes before return.

Actually in the middle. We talked about this class.
C: Deformation of exception handling
Try...catch...finally
Try...catch ...
TRY...CATCH...CATCH...:JDK7 can be improved later:
try{
int a=3/0;
int b=arr[5];
}catch (arithmeticexception| ArrayIndexOutOfBoundsException e) {
SYSTEM.OUT.PRINTLN ("problem");
}
Try...catch...catch...fianlly
Try...finally: The purpose of this approach is to release resources.
(6) Custom exceptions
Inherited from exception or runtimeexception, it is only necessary to provide a non-parametric construction and a parameter structure.
Cases:
public class MyException extends exception{
Public myexception () {}
Public myexception (String message) {
Super (message);
}
}
(7) Precautions for exception
A: The parent's method has an exception thrown, and the child's overriding method must be less than or equal to the parent exception when throwing an exception, and the recommendation is the same as the parent class exception
B: The parent's method does not throw an exception, the child's overriding method cannot have an exception thrown, if there is an exception in the subclass method, then only try cannot throws.
C: The parent's method throws more than one exception, and the child's overriding method can only throw the same exception or subclass exception of the parent exception, and cannot throw away the parent class.
(8): How to handle exceptions
Principle: If the problem can be handled within the function, there is a try, if not handled, referred to the caller, that is, with throws
Difference: Subsequent programs need to be run on the try. Subsequent programs do not need to continue to run throws.
Example: A cold to take their own medicine can be good try. Eat for a long time is not good, throws to the hospital. The hospital can't cure the error.

Dark Horse programmer----Java Base---exception, with related questions

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.