Exception mechanism
The basic concept
- An exception means “abnormal” and in the Java language refers to an abnormal condition that occurs during the execution of a program.
- The java.lang.Throwable class is the Java language superclass for errors and exceptions.
- The Error class is mainly used to describe serious errors that cannot be resolved by the Java virtual machine, such as JVM hanging.
- The Exception class is mainly used to describe minor errors caused by programming errors or accidental external factors, which can be solved by coding, such as 0 as a divisor.
Classification of anomalies
- The java.lang.Exception class is a superclass for all exceptions, which fall into two main categories: IOException and other exceptions – Other exceptions are also called detectability exceptions. Detectability exceptions are those that can be detected by the compiler at compile time.
- The main subclasses of the RuntimeException class are: ArithmeticException class – arithmetic exception ArrayIndexOutOfBoundsException class – the array subscript cross-border abnormal NullPointerException – null pointer exception ClassCastException – Type conversion exception NumberFormatException – Number format is abnormal
- Note: When an exception occurs during program execution but is not handled manually, the Java virtual machine handles the exception in the default way: printing the name of the exception, the cause of the exception, the location of the exception, and terminating the program.
Exception avoidance
- In future development, try to use if condition judgment to avoid the occurrence of exceptions.
- However, too much if condition judgment will lead to program code lengthening, bloated, poor readability.
Exception catching
- Syntax format
try {
// Write code where exceptions can occur;
}
catch(Exception type references variable name) {// Write the exception handling code for this class;
}
finally {
// Write code that executes regardless of whether an exception occurs;
}
Copy the code
- Matters needing attention
- When writing multiple catch branches, remember that small types should come before large ones;
- Lazy people write:
catch(Exception e) {}
- Finally is usually used for cleanup, such as closing open files.
- Execute the process
try {
a;
b; // Possible exception statement
c;
}catch(Exception ex) {
d;
}finally {
e;
}
// If no exception occurs, the execution process is: a b C e;
A, b, d, e;
Copy the code
Exception throwing
- Basic Concepts When an exception cannot be handled or easily handled under special circumstances, it can be passed to the caller of the method. This method is called exception throwing. When an exception occurs during method execution, the underlying generation of an exception class object is thrown, and the subsequent code of the exception code is no longer executed.
- Syntax format
Access Permission Returned Value Type Method name (parameter list) throws Exception type 1, exception type 2,… {method body; }
Such as:
public void show() throws IOException{}
- The principle of method rewriting
- It requires the same method name, argument list, and return value type. Starting in JDk1.5, return subclass types are supported.
- Method access permissions cannot be smaller, can be the same or larger;
- Require methods not to throw larger exceptions;
- Note: methods overridden by subclasses cannot throw a larger exception or a different level exception, but they can throw the same exception, a smaller exception, and no exception.
- Share If the overridden method in the parent class does not throw an exception, the overridden method in the child class can only catch the exception. If several other methods are called inside a method in a progressive manner, it is recommended that these methods be handled with thrown methods to the last layer for capture processing.
Custom exception
- Basic Concepts When there is a need to express age-inappropriate situations in programs, and Java does not officially provide such targeted exceptions, the programmer needs to customize the exception description.
- The implementation process
- The custom xxxException Exception class inherits the Exception class or its subclasses.
- Provides two versions of the constructor, one that takes no arguments and one that takes a string as an argument.
- Occurrence of exceptions
Throw new Exception type (argument);
Such as:
Throw new AgeException(" Age incorrect!! ") );
- Java uses the exception handling mechanism is the exception handling program code together, separated from the normal program code, making the program concise, elegant, and easy to maintain.
The File type
The basic concept
- The java.io.File class is used to abstract the path of a File or directory. You can obtain characteristic information about a File or directory, such as its size.
Common methods
Method statement | Functions overview |
---|---|
File(String pathname) | Constructs an object from the pathname specified by the argument |
File(String parent, String child) | Constructs an object based on the parent path and child path information specified by the argument |
File(File parent, String child) | Constructs an object based on the abstract parent path and child path information specified by the argument |
boolean exists() | Tests whether the file or directory represented by this abstract pathname exists |
String getName() | Get the name of the file |
long length() | Returns the length of the file represented by this abstract pathname |
long lastModified() | Used to get the time when the file was last modified |
String getAbsolutePath() | Obtain the absolute path information |
boolean delete() | Used to delete files. The directory must be empty |
boolean createNewFile() | Use to create a new empty file |
boolean mkdir() | Used to create directories |
boolean mkdirs() | Used to create multi-level directories |
File[] listFiles() | Gets all the contents of the directory |
boolean isFile() | Check whether it is a file |
boolean isDirectory() | Check whether it is a directory |
File[] listFiles(FileFilter filter) | Gets everything in the directory that satisfies the filter |