Every programmer may encounter various exceptions during programming. What should we do when an exception occurs? What specific handling methods should we take for different exceptions? These problems have not been quite clear to me for a long time since I started learning programming. Fortunately, as I continue to learn, I gradually learned something about it. Let's take a look at various exceptions in Java.
Let's take a look at the figure below:
All exceptions in Java are inherited from Throwable. Throwable is defined in the jave. lang package and has two direct subclasses: Exception and Error. Here, Error refers to the internal system Error of the Java virtual machine, and we usually cannot handle such errors. Today we mainly discuss Exception. All other exceptions are subclasses of Exception, and only these exceptions can be handled by programmers.
As shown in, Exception has two important direct subclasses: IOException and RuntimeException. The IOException class is used to handle program input and output exceptions, including keyboard locking or file exceptions. The RuntimeException class is used to handle errors generated from program logic, for example, access to the array subscript out of bounds, null pointer exception, etc.
Common exceptions:
Exception class name |
Exception description |
ArithmeticException |
Arithmetic exception |
ArrayIndexOutOfBoundsException |
Array subscript out-of-bounds exception class |
ArrayStoreException |
An exception thrown when an incompatible value with the array type is assigned to an array element. |
ClassCastException |
Type forced conversion exception |
ClassNotFoundException |
Corresponding class exception not found |
EOFException |
File ended exception class |
FileNotFoundException |
File No exception class found |
IllegalAccessException |
Exception thrown when access to a certain type is denied |
InstantiationException |
Exception thrown when trying to create an instance of an abstract class or abstract interface through the newInstance () method |
IOException |
Input/Output exceptions |
NegativeArraySizeException |
Create an array exception class with negative element count |
NullPointerException |
Null Pointer exception class |
NumberFormatException |
An error occurred while converting a string to a number. |
NoSuchFieldException |
Field no exception class found |
NoSuchMethodException |
No exception class found in Method |
SecurityException |
An exception thrown when the Applet executes the action prohibited by the browser's Security Settings |
SQLException |
Database Operation exceptions |
StringIndexOutOfBoundsException |
An error occurred while the string index was out of the range. |
IndexOutOfBoundsException |
This exception is thrown when the index of an object (such as an array or string) is out of the range. |
1. if an exception cannot be handled, do not capture it;
2. Capture exceptions in ascending order (for example, capture NullPointerException first, RuntimeException, and finally Exception );
3. Try to catch the exception near where the exception is thrown;
4. Record the exception to the log unless you want to throw it again;
5. construct your methods according to how detailed your exception is to be handled;
6. Several types of exceptions are required, especially for application exceptions.
Capture appropriate exceptions
Correct Handling of exceptions is not an easy task, because exceptions sometimes cause other unknown behaviors in the program. However, the following three rules can help you avoid the possible risks of error handling exceptions.
Rule #1: Always capture the type of the thrown exception and ignore the exception superclass. To comply with common code habits, you can use uppercase letters of the Exception class as variable names, as shown below:
Catch (FileNotFoundException fnfe)
And
Catch (SQLException sqle)
Rule #2: Never leave the catch Block empty. In many cases, although a try/catch Block is compiled, nothing is done in the catch part of the code. Alternatively, if you use the log API (Logging API), write the code to write the exception to the log.
Rule #3: Never throw an instance of the Exception base class. Developers should always throw their own exception classes.
OK. The above is my summary of some learning experiences on Java exceptions. I hope I can help anyone who reads this article a little bit.