Java Exception Handling-Basics
Exception handling, in the English name of exceptional handling, is a mechanism in programming languages or computer hardware, used to handle exceptions in software or information systems (that is, exceptions that exceed certain special conditions for normal execution of the program ). Exception Handling separates the receiving and processing of error codes. The exception handling (also known as error handling) function provides a way to handle any exceptions or exceptions that occur when the program is running. Exception Handling uses the try, catch, and finally keywords to try operations that may fail, the processing fails, and the resources are cleared afterwards.
Exception Handling category
Concept:
Error: Error generated and thrown by the Java Virtual Machine, including dynamic link failure and virtual machine Error (not processed by the Program)
Exception: the parent class of the Exception class, including compile-time errors and runtime errors. Its subclass corresponds to a variety of possible Exception events, users must explicitly declare and capture (users need to process)
RuntimeException, explicit declaration or capture will significantly affect program readability and running efficiency. The system automatically detects and instructs them to the default exception handler (not required by the user)
Error and Exception:
Error:
1. Always uncontrollable
2. It is often used to indicate system errors or low-level resource errors.
3. If possible, it can be captured at the system level.
Exception:
1. It can be controllable or uncontrollable.
2. Errors Caused by programmers
3. It should be processed in the application assembly
Exception capture and handling
The capture and exception of exception handling mainly include the following keywords: try, catch, finally, throw, and throws.
When an exception occurs, the program terminates the current process and executes the catch code segment based on the type of the exception obtained. The finally code segment is executed no matter whether an exception occurs, release resources after execution.
Import java. io. *; public class TestEx {public static void main (String [] args) {int [] arr = {1, 2, 3}; System. out. println (arr [2]); try {System. out. println (2/0);} catch (ArithmeticException AE) {System. out. println (the system is being maintained. Contact the Administrator !); AE. printStackTrace () ;}finally {System. out. println (catch successful !);}}}
The running result is as follows:
Difference between throw and throws
Throw:
1. throw is an exception thrown by the statement.
2. appear in the function body
3. After writing throw, the program will certainly execute this program
Throws:
1. throws is a declaration that a method may throw an exception. When declaring a method, this method may throw an exception.
2. throws appears in the method function.
3. After throws is written, exceptions may not be thrown, and exceptions may not occur.
Summary:
Exception Handling separates the error code, making the program more rigorous and the program write more neatly. At the same time, you can pass errors along the call stack, so that you can quickly find the cause of the errors and facilitate future maintenance.