This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
1 Abnormal Mechanism
1.1 Basic Concepts
- 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.
1.2 Classification of exceptions
-
The java.lang.Exception class is a superclass for all exceptions, which fall into two main categories:
- Runtim eException – Runtime exception, also known as a non-detection exception
- IOException and other exceptions – Other exceptions, also called detectability exceptions, detectability exceptions are exceptions that can be detected by the compiler at compile time.
-
The main subclasses of the Runtim eException class:
- Arithmeticexception class – Arithmetical exception
- ArrayIndexOutOfBoundsException class – an array subscript cross-border anomalies
- NullPointerException – NullPointerException
- ClassCastException – Type conversion exception
- Num berForm atException – Number format exception
-
Note:
If an exception occurs during program execution but is not handled manually, the Java VM uses the default method to handle the exception: printing the name, cause, location, and termination of the program.
1.3 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.
1.4 Catching exceptions
- Syntax format
try{write code where exceptions may occur; }catch(The exception type refers to the variable name) {write the handling code for the exception of this class; }... finally {write code that is to be executed whether or not an exception occurs; }Copy the code
- Matters needing attention
- A. When writing multiple catch branches, remember that small types should come before large ones;
- B. catch(Exception e) {}
- C. finally is often used for post-processing, such as closing files that have been opened.
- Execute the process
try{ a; b; - Statements where exceptions may occur c; }catch(Exception ex) { d; } fi nally {e; }Copy the code
Execution process when no exception occurs: A, B, C, E; The execution process when an exception occurs: A, B, D, E;
1.5 Throwing exceptions
- The basic concept
When there are special circumstances in which an exception cannot be handled or is inconvenient to handle, the exception can be passed to the caller of the method. This method is called exception throwing. When an exception occurs during the execution of a method, an exception class object is generated at the bottom and thrown, and the code following 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; }
public void show(a) throws IOException{}
Copy the code
-
The principle of method rewriting
- A. The method name, parameter list, and return value type must be the same. Starting from JDk1.5, return subclass types are supported.
- B. The access permissions of methods can be the same or larger.
- C. Require methods not to throw larger exceptions;
-
Note:
A method overridden by a subclass cannot throw a larger exception, cannot throw an exception of a different level, but can throw the same exception, a smaller exception, and no exception.
-
conclusion
- 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.
1.6 Custom Exceptions
- The basic concept
When it is necessary 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
- A. Customize the xxxException Exception class to inherit the Exception class or its subclasses.
- B. Provide two versions of the constructor, one that takes no arguments and the other that takes strings as arguments.
- Exception generation: throw new exception type (argument);
throw new AgeException("Age is not reasonable!!");
Copy the code
- 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.
2 the File type
2.1 Basic Concepts
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.
2.2 Common methods
Method statement | Functions overview |
---|---|
File(String pathnam e) | 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 getNam e() | Get the name of the file |
long length() | Returns the length of the file represented by this abstract pathname |
Long lastModi fi Ed () | 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 |
The File [] listFiles (FileFilter fi lter) | Gets everything in the directory that satisfies the filter |