I. Exception Mechanism
(1) exception Overview
1. Exception: The program is abnormal during running.
2. exception class: Describes the abnormal situation when the program is running in the form of a Java class. And encapsulated into objects.
(2) exception Classification
Exception classification: "serious error" and "non-serious exception"
(1) One is serious: For serious problems, Java describes them through the error class.
(2) One is not serious: for non-serious ones, Java describes them through the exception class. For exceptions, you can use targeted processing methods.
(3.)Exception System
1. throwable
| --- Error: the error class cannot be processed and the JVM throws it.
| --- Exception: You can define targeted processing.
2. Features of the exception system
(1) All classes in the exception system and the objects created are throttled.
(2) It can be operated by the throw and throws keywords.
(3) only the exception system has this characteristic.
(4) Requirements for Exception Handling
1. When declaring an exception, it is recommended to declare a more specific exception so that it can be handled more specifically.
2. When the other party declares several exceptions, it corresponds to several catch blocks. Do not define unnecessary catch blocks. If an exception in multiple catch blocks has an inheritance relationship, the parent exception catch block is placed at the bottom.
Suggestion: The specific handling method must be defined in catch. Do not simply define e. printstacktrace (). Do not simply write output statements.
(5) exception handling statement
1. Java provides special statements for processing.
Try {
Code to be detected
} Catch (exception variable ){
Code for handling exceptions;
} Finally {
Statements that will be executed
}
There are three combined formats:
(1) Try {} catch (){}
(2) Try {} finally {}
(3) Try {} catch () {} finally {}
Note: (1) In finally, resource code is usually disabled. Because resources must be released. (2) If a function defines some code that must be executed, you can use try {} finally {} to put some executed code in the finally code block. (3) Finally is not executed in only one case. When it is executed to system. Exit (0); fianlly will not execute.
(6)ThrowAndThrowsUsage
Throw is defined in the function and is used to throw an exception object.
Throws are defined on functions to throw exception classes. Multiple throws can be thrown and separated by commas.
When throw throws an exception object in the function content, no try is performed. Must be declared on the function; otherwise, the compilation fails. Note: Except runtimeexcpetion.
(7)ThrowAndThrowsDifference
1. Throws are used in functions, while throw is used in functions.
2. Throws are followed by exception classes, which can be separated by commas. Throw is followed by an exception object.
(8) Handling of thrown exception information
1. When throw throws an exception object in the function, the corresponding processing action must be given. Either try catch internally. You can either declare it on the function for the caller to process it.
2. Generally, an exception occurs in the function and must be declared on the function. The throws keyword declares that the function may have an exception type.
Note: exception has a special subclass runtimeexception. If a runtimeexcpetion exception is thrown in the function, the function does not need to be declared, and compilation passes. If the runtimeexcpetion exception is declared on the function, the caller does not need to handle the exception. Compilation is the same.
3. You do not need to declare the function because it does not need to be processed by the caller. When this exception occurs, you want the program to stop. Because the operation cannot be continued during running, you want to modify the code after stopping the program.
4. If the function declares an exception, the caller must handle it. The processing method can be throws or try.
5. Perform common operations on caught exception objects:
(1) string getmessage (); // obtain the exception information. Returns a string.
(2) tostring (); // gets the exception class name and exception information, and returns a string.
(3) printstacktrace (); // obtain the exception class name and information, as well as the location where the exception occurs in the program. Return Value void. The default Exception Handling Mechanism of JVM is to call the printstacktrace method to print the trace information of the abnormal stack.
(4) printstacktrace (printstream s) // This method is usually used to save the exception content in the log file for reference.
Ii. Custom exceptions
This is because the project has special problems, which are not described by Java and the objects are encapsulated. Therefore, we can follow the object-oriented thinking in Java to address these special problems. Encapsulate custom exceptions for specific problems. The definition class inherits exception or runtimeexception.
1. To make the custom class writable.
2. Let this class have common methods for abnormal operations.
When you want to define information about custom exceptions, you can use the features defined by the parent class. The exception information is passed to the constructor of the parent class. Because the operation on the exception information has been completed in the parent class. Therefore, as long as the Child class is constructed, the exception information is passed to the constructor of the parent class. Through the super statement, you can directly obtain the custom exception information through the getmessage method.
1 class myexception extends exception 2 2 2 {3 private string MSG; 4 myexception (string MSG) {5 super (MSG); // the input information will be returned 12 6} 7}
Note: Custom exceptions must be inherited from custom classes. Generally, exceptions are inherited.
Exception must be inherited because the exception system has a characteristic that exception classes and exception objects are thrown. They are both flexible. This flexibility is unique in the throwable system. Only classes and objects in this system can be throws and throw operations.
Iii. Benefits and handling principles of exceptions
(1) benefits of exceptions
1. encapsulate the problem;
2. Separate the normal process code from the problem handling code to facilitate reading.
(2) Principles of Exception Handling
1. There are two methods: Try and throw.
2. When you call the function to throw an exception, you can handle a few throws. A try corresponds to multiple catch.
3. When multiple catch entries exist, put the catch of the parent class at the bottom. Otherwise, an error is reported during compilation because other catch statements cannot be executed.
4. Define targeted processing methods in catch. Do not simply define printstacktrace and output statements. Do not leave it empty. If the caught exception cannot be handled by this function, it can be thrown in catch. The sample code is as follows:
1 try {2 throw new aexception (); 3} catch (aexception e) {4 throw E; // If the exception cannot be handled, it does not belong to the exception of this function. 5}
5. The exception can be handled. When an exception is generated and problems related to this function need to be provided, the caller can know. And process. You can also convert the caught exception to a new one. For example, in the remittance example, if the ATM fails when transferring money to someone else, you can find another place to transfer the money or tell the recipient that the transfer was unsuccessful. The sample code is as follows:
1 try {2 throw new aexception (); 3} catch (aexception e) {4 // process aexception. 5 throw new bexception (); 6}
Iv. Precautions for exceptions
(1) When the child parent class overwrites
1. The exception thrown by the subclass must be a subclass or subset of the parent class exception or a parent class exception.
2. If the parent class or interface does not throw an exception when there is no exception, the subclass overwrite exception can only be thrown by try.
3. If the parent class throws multiple exceptions, the Child class can only throw a subset or Child class of the parent class exception.
4. The parent class method does not throw an exception, and the subclass cannot throw it.
(2) It is not necessary to declare that the problem can be solved internally.
(3) catch is used to handle exceptions. If no catch is found, the exception is not handled. If the exception is an exception during detection. It must be declared.
I. Package (Package): It is equivalent to a folder and is a form of encapsulation. The package name can be multilevel.
Ii. Functions of packages
1. Avoid duplicate names of multiple classes. If two classes with the same name appear, you can use the package to distinguish them to avoid conflicts.
2. Manage class files by category. You can put related classes in the same package.
3. multiple namespaces are provided for classes.
4. When a package appears, Java class files and source files can be separated.
Iii. Writing rules
1. The package name must be written in the first line of the program file.
2. Full name of the class file: package name. Class Name.
4. Access between packages
1. access between the package and the package. The classes in the accessed package and the Members in the class must be public.
2. subclasses in different packages can directly access members modified by the protected permission modifier in the parent class.
3. There are only two types of permissions available for access between packages: public and protected.
V. Permissions
|
Public |
Protected |
Default |
Private |
In the same class |
OK |
OK |
OK |
OK |
In the same package |
OK |
OK |
OK |
× |
Subclass |
OK |
OK |
× |
× |
In different packages |
OK |
× |
× |
× |
6. Package import (Keyword:Import)
1. You can simplify the class name. When calling classes in other packages, you need to write the full name of the class, that is, together with the package name.
2. Import the classes in the package. Do not import the packages in the package.
3. A program file contains only one package and can have multiple imports.
Suggestion: (1) When importing a package, if there are many classes in the package, you can use wildcard * to replace all classes in the package. However, we recommend that you do not use wildcard * because importing unused classes will occupy the memory space. All the classes in the package to be used when writing the program will be imported. (2) The definition package name should not be repeated. You can use a URL to complete the definition. The URL is unique. (3) When the imported packages have the same class, you must write the full name of the class to differentiate them. Otherwise, an error will be reported.
VII. jar package
(1)JarPackage
More and more classes, we can use packages to install. When there are more and more packages, We can compress the packages. Java uses the jar tool to compress the package, and the compressed suffix is jar.
(2) SomeJarCommand
1. Create a jar package: jar-CVF mypack. Jar PACKA packb
2. view the jar package: jar-tvf mypack. Jar [> targeted file]
3. decompress: jar-xvf mypack. Jar
4. The configuration file of the custom jar package: jar-cvfm mypack. Jar mf.txt PACKA packb
Dark Horse programmer-object-oriented (2) exception mechanism, package)