Main content
Introduction to 1.JAVA Exceptions
2 . Use try: Catch.. Finally implementing exception handling
3.exception throws in Java and custom exceptions
4. Abnormal chain
5. Summary of Experiences
Introduction to Java Exceptions:It is different from the normal, and it is not the same, there are errors. An issue that prevents the current method or scope from continuing to execute is called an exception. Exceptions in Java: 1 All abnormal classes in Java inherit from the Throwable class, throwable mainly have two large classes, one is the error class, and the other is the exception class. 2 where the error class includes virtual machine errors and thread deadlocks, once the error occurs, the program is completely dead, called the program Terminator. 3 Exception class mainly refers to the coding environment, the user operation input problems, exception mainly include two major categories, non-check exceptions (runtimeexception) and check exceptions (some other exceptions) its subclasses are: Non-check exceptions ( Runtime exception runtimeexception): "Automatically captured by Java virtual machine" such as null pointer Nullpointer, out-of-bounds arrayindexoutofbounds, error type conversion classcast, arithmetic exception arithmetic, etc. · Check exception checkexception: "Need to manually add capture and Processing statements" file exception io, such as exception handling: to prompt the exception to the user or programmer, so that the program has been interrupted, in an appropriate way to continue to run or exit and be able to save the user's current operation, or data rollback , and finally release the resources that are consumed.Use try: Catch.. Finally implements exception handling:One, try-catch (multiple catch blocks can have multiple catch blocks to catch multiple exceptions)-finally (1) Try block: is responsible for catching the exception, once an exception is found in the try, the control of the program will be handed over to the exception handler in the CATCH block. "Try statement block can not exist independently, must be the same as catch or finally block" (2) Catch block: How to handle? For example, warn: prompt, check configuration, network connection, record error, etc. After executing the catch block, the program jumps out of the catch block and proceeds with the subsequent code. Note for writing catch blocks: The exception class that is handled by multiple catch blocks is treated as if the parent class was handled by a catch subclass, because the exception is handled "nearest" (from top to bottom). (3) Finally: The final execution of the code, used to close and release resources such as exception handling Try-catch and try-catch-finallytry{//Some exceptions will be thrown}catch (Exception e) {//code block handling the exception} finally{//eventually executes code}try throws many types of exceptions, and multiple catch blocks capture multiple-clock errors. Multiple exception handling code block order problem: Subclass again Parent class (wrong order will also alert error), finally statement block handles the code that will eventually executeexception throws in Java and custom exceptions:Throw---Throws an exception (action) throws---declares what type of exception (declaration) will be thrown (declared) public void method Name (argument list) throws exception list {//call will throw exception method or throw new Exception (); ---throws an exception first} Two important keywords: the exception list for throw and throws1.throws can either throw an exception or throw multiple exceptions, each of which is separated by commas in the middle of 2. The method in which an exception is called in the body of the methods or an exception is thrown first: with the throw new Exception () throw is written in the method body, which means "throw exception" action 3. When a method invokes a method that throws an exception, it must call the try Catch statement block to attempt to handle exception 4. When the caller does not know or does not have to handle the exception, it continues to declare that an exception will be thrown, processing 5 for the more than one level of the caller. Although Java provides a large variety of exceptions, we use custom exceptions when actually used: Custom exceptions: either inherit from a similar exception , or inherit from the base class of all exception classes exception
Custom Exceptions:Class custom exception class extends exception{};Exception Chain:Sometimes, we can wrap the caught exception into a new exception, then add a reference to the original exception inside the new exception, and then throw the new exception, they are like a chain reaction. The basic way to implement the function of the exception chain: the second notation:
Experience Summary:
1, when dealing with abnormal operation, using logic to avoid the simultaneous auxiliary try-catch processing
2. After multiple catch blocks, you can add a catch (Exception) to handle the exceptions that may be missed.
3, for the indeterminate code, you can also add Try-catch processing potential exceptions
4, as far as possible to deal with exceptions, do not just simple reference printstacktrace () to print out.
5, how to deal with the exception, according to different business needs and the type of exception to decide
6, try to add a finally statement block to release the resources occupied
7, use the function to distinguish the function blocks.
Exception and exception handling in Java