Exception: Is the description of the problem, the problem is encapsulated object,
Abnormal system:
Throwable:
1.Error
2.Exception
1.RuntimeException
Characteristics of the anomaly system:
All classes in the exception system and the objects created are parabolic.
Can be manipulated by the throw and throws keywords, only the exception system has this feature.
Differences in the usage of throw and throws:
A throw is defined inside a function and is used to throw an exception object.
Throws is defined on a function that throws an exception class and can be thrown multiple and separated by commas.
When the function content has a throw throw exception object, not Trycatch processing, must be declared on the function throws, otherwise the compilation will fail.
Except for RuntimeException, the function can be declared without a runtimeexcpetion exception if it is thrown inside the function.
Exception Handling Statements:
Try
{
Code that needs to be detected;
}
catch ()
{
The code that handles the exception;
}
Finally
{
Code that is bound to execute;
}
Finally: code that is bound to be executed, usually defined as the code that is used to close the resource.
Special case: System.exit (0) was executed before the finally statement; Statement, the finally statement is not executed.
Custom Exceptions:
Define class inheritance exception or RuntimeException
1. In order to make the custom class can be parabolic.
2. Let the class have a common approach to operating exceptions.
When you want to define the information for a custom exception, you can use the functionality already defined by the parent class. Pass the exception information to the constructor of the parent class.
Class Xxxexception extends Exception
{
Xxxexception (String message)
{
Super (message);
}
}
Custom exceptions: Encapsulate specific issues that arise in your program according to the object-oriented thinking in Java.
When the child parent class overrides:
1. The exception thrown by the subclass must be a subclass or a subset of the exception of the parent class.
2. If the parent class or interface does not throw an exception, the subclass overrides an exception that can only be trycatch processing and cannot be thrown with throw/throws.
1 /*2 the area of the circle, and the rectangle3 */4 classNovalueexceptionextendsRuntimeException//Exception5 {6 //private String message;7 novalueexception (String message)8 {9 Super(message); Ten } One } A - /* - Abstract class Shape the { - abstract void Getarea (); - } - + Class Rec extends Shape - */ + //interfaces are also an abstract approach A InterfaceShape at { - voidGetarea (); - } - classRecImplementsShape - { - Private intLen,wid; inRec (intLenintWid//throws Novalueexception - { to if(Len<=0 | | Wid<=0) + Throw NewNovalueexception ("Len,wid value is illegal"); - /* the { * System.out.println ("Len,wid value is illegal"); $ }Panax Notoginseng */ - the This. Len =Len; + This. wid =wid; A the } + Public voidGetarea () - { $System.out.println ("The area of the rectangle is:" +len*wid); $ } - } - classCircleImplementsShape the { - Private Doubleradius;Wuyi Public Static Final DoublePI = 3.14; theCircle (DoubleRadiusthrowsnovalueexception - { Wu if(radius<=0) - Throw NewNovalueexception ("The case of illegal values has occurred"); About $ This. Radius =radius; - } - Public voidGetarea () - { ASystem.out.println ("The area of the circle is:" +radius*radius*PI); + } the - } $ the classExceptionTest2 the { the Public Static voidMain (String args[]) the { - /* in //exception is inherited. the Try the { About Rec r = New Rec ( -3,4); the R.getarea (); the } the catch (novalueexception e) + { - System.out.println (e.tostring ()); the } Bayi */ the //when you inherit runtimeexception, you do not need to do a try operation. An exception occurs when the program stops. theRec r =NewRec (3,4); - R.getarea (); - the the Try the { theCircle C =NewCircle (-3); - C.getarea (); the } the Catch(novalueexception e) the {94 System.out.println (e.tostring ()); the } the the 98 About - }101 102}
Package: Packages
Function: Separates the source file from the class file.
Format:
Package name; The definition statement is to be defined in the first line of code. (top)
With the package name, the name of the class is the name of the package. Class name (the class to be on the package)
When classes in a package inherit from each other, you can access the properties and methods that are protected decorated. There are two types of permissions that can be used between packages and packages: public and protected
The ability to access the four types of permissions is as follows:
Public protected default Permissions private
Ok ok OK in same class
In the same package ok OK No
Sub-class OK OK no no
Different Packages Ok no no No
Note: In a Java file, you cannot have two or more than two public classes or interfaces.
Imports: Import keyword.
function to simplify the writing of the class name. When import, the class name can be shortened directly, not in the form of a package name. The name of the class is written.
Object-oriented seems to finally learn, many still do not understand, but time waits for people, can only study the side of the back to continue to see object-oriented knowledge.
Java Basic Notes-Exception summary, package