JAVA14
Exception Introduction
The problem with Java code that occurs during runtime is an exception
L encapsulate exception information in Java as a class
• When a problem occurs, an exception class object is created and the exception-related information is thrown
L If the location of the abnormal occurrence of reasons, etc.
Inheritance system
L Java.lang
Class Throwable can be thrown out of class
L All errors and exceptions in Java
L known sub-class directly:
Error, Exception
Differences between exceptions and errors
L array access out of bounds is an exception
Exception RuntimeException
Compile, can be handled in a specific way during runtime
L array space too large is wrong
Error
An error occurred during Operation no specific method of handling system-level issues are occurring in the system where the JVM resides, and feedback to the JVM can only modify the code
Cause of the exception and how to handle it
L
L method Internal problem The JVM has detected an exception to the object that created the corresponding exception
The L method cannot handle the caller who throws the exception object to the method inside
The caller of the L method cannot handle the object and throws it to the caller of the main method.
L is the JVM virtual machine and the JVM receives the exception
L then the JVM outputs the exception information in a red font to the console to stop the program at the calling method code
Handling Throw keyword Processing
L Use the method to consider the occurrence of anomalies need to determine the parameters passed to see if it is legal if it is illegal to tell the caller, it is necessary to throw an exception in the way to tell
L Throw an exception inside a method
l Example
- Perform a judgment throw exception throw new Exception ("info")
- Declare exception after method name with throws exception type
- method calls should take into account the method of the exception of the call when the exception to the method of processing, such as throwing exceptions to the upper level such as the main method thrown to the virtual machine
Try ... catch...finally processing
L format
Try {potentially unexpected code} catch {exception handling} finally {code that must be executed}
l Example
- The main method executes the array definition
- Enter try code block
- Calling methods
- Method execution judgment null create your own well-written exception class object
- Throwing an exception object to a program that is behind a method caller no longer executes
- The method caller's try detected an exception occurred and received an exception object
- Throws the exception object to the catch code block to handle the exception
- Catch catch Capture
- Catch can catch a corresponding type of exception
- Catch catches to this exception for processing
- Exception handler continues execution
Multiple catch capture
L can handle multiple exceptions
L Precautions
- Catch-write exceptions have sequential concepts
- Peer exception: No inheritance relationship between thrown exception classes
- There is no order for peer exceptions
- Ctrl-T Inheritance tree
- The exception of the subordinate relationship must be the higher the parent class is written below
- Otherwise the compilation will error
- Low-level catch is useless.
Finally code block
L code that must be executed
Execute the block of code, whether or not an exception occurs
L used to release resources
Try...catch ... and throws how to choose
L used a method of throwing an exception to use Try...catch ...
l Otherwise, it will cause the program to stop if it is thrown upwards.
Compile exception and run-time exception
L Compile exception
- Calling a method that throws an exception fails to process the compilation (try throws)
L Run exception
- The exception that is thrown is the RuntimeException class or his subclass, which are run-time exceptions.
- What is thrown inside a method is a run exception and does not require a throws declaration
- The caller also does not need to process
- Design reasons
A) operation exception cannot occur
b) But if the programmer needs to stop the program to modify the source code
c) Run exception if the following code is not meaningful
Case
L Calculate circular Area
L to invoke run-time exception handling with a radius of 1
Exception handling When overridden by a method
L The method of the parent class throws an exception
- Subclasses can not throw exceptions after overriding
- You can also not throw exceptions
- If the subclass throws an exception, the exception object cannot be greater than the exception of the parent class
- Subclasses also cannot throw a peer exception
- Reason
The parent class calls the original method of the parent class when a polymorphic call method
- The parent class throws an exception subclass and cannot throw an exception
- The subclass method throws an exception when the method in the parent class is called to throw an exception that can only be thrown with a try catch and cannot be thrown up because the parent class does not throw an exception
Throwable class method
L String GetMessage ()
A detailed description of the exception information
L String toString ()
A short description of the exception information
L Void printstacktrace () virtual machine default calling method
Tracing the exception information to the standard error stream
l Example
Custom Exception Ingestion
Java pre-defined a lot of exceptions
l have some exceptions.
l need to write it myself.
L For example negative scores
L Format xxxexception
L Custom exception classes need to inherit the parent class exception
Example
L Calculate the average of the scores. Abnormal stop operation with negative number
L
L
L
L Most use Run-time exceptions
l need to write null parameter constructor with parameter constructor
L Call the parent constructor with super in the parameter constructor
The Java self-study road-DAY14