Analysis of exception in Java programming and its object-oriented thinking summary [figure]

Source: Internet
Author: User

Analysis of exception in Java programming and its object-oriented thinking summary [figure]
1. Exceptions:
Abnormal behavior that occurs in the program.
2. The origin of the exception:
Program in the process of running the abnormal situation, the program sees it as an object to extract attribute behavior (name, reason, location and other information)
form a variety of exception classes
3. Classification of Exceptions: (Throwable)
1.Error (Error): A critical error in the run that does not require us to make any changes.
2.Exception (): No serious errors occur in the run, we can try to change them.
4.Exception: Category:
The first Category: System Exceptions: The system is defined in advance and we use it directly
Custom Exceptions: We need to define our own
Second Category: Compile exception: Throws an exception during compilation and handles exceptions
Runtime exception: Throws an exception at run time, handles exception
public class Demo8 {
public static void Main (string[] args) {//4. There is no ability to handle the exception, continue to throw up, throw it to the JVM (Java Virtual machine)
The JVM's approach: to call the exception object's printing method, the name of the exception, location, reason to print to the console
Math math = new Math ();
Math.div (2, 0);//3. Thrown here, there is no ability to handle anomalies, continue to throw upward, throw to main
}
}
Class math{
public int div (int a,int b) {//2. Thrown here, there is no ability to handle the exception, continue to throw up, throw to the location of the calling method
Return A/B;//1. Create an exception object with a divisor of zero (new ArithmeticException ()), there is no ability to handle exceptions
Throw an exception up and throw it in the way it is
}
}

5. Characteristics of the anomaly:
When the program has multiple exceptions, the program prints the exception information and interrupts the program, so when multiple exceptions occur at the same time, only the first one can be executed.
6. Common exceptions
public class Demo2 {
public static void Main (string[] args) {
int[] arr = new int[]{4,5,6};
Array subscript out-of-bounds exception arrayindexoutofboundsexception
System.out.println (Arr[7]);
arr = null;
Null pointer exception NullPointerException
System.out.println (Arr[0]);
}
}
7. Handling of exceptions
Detect exceptions, catch exceptions, and let exceptions do not affect the execution of the following code.
* try{
* Code that may appear abnormal
*}catch (Exception e) {//catch exception E is the exception to be caught
* Processing of code that appears to be abnormal
* }
*
* Continue to perform the following normal code
public class Demo3 {
public static void Main (string[] args) {
Math math = new Math ();
try {
int value = Math.div (4, 0);//3. Throw it here. New ArithmeticException ()
As long as the code inside the try has an exception, the catch catches the exception immediately, so the code here does not execute.
Only the code inside the try does not have an exception, and the code here executes.
System.out.println ("Value:" +value);
} catch (Exception e) {//4. Catch exception: E = new ArithmeticException ()
E.printstacktrace ();//can print the location of the exception, reason, name and other information
System.out.println (E.getmessage ());//The Cause of the exception
System.out.println (E.tostring ());//The name of the exception, the reason
System.out.println ("Code to handle the exception");
}
System.out.println ("Go on");
}
}
Class math{
public int div (int a,int b)//2. Throw it here. New ArithmeticException ()
{
Return A/B;//1. Generate and throw new ArithmeticException ()
}
}
8. Handling of multiple exceptions
* try{
* Code that may appear abnormal
*}catch (Exception one E) {//catch exception E is the exception to be caught
* Processing of code that appears to be abnormal
*}catch (Exception two E) {//catch exception E is the exception to be caught
* Processing of code that appears to be abnormal
*}catch (Exception e) {//catch exception E is the exception to be caught
* Processing of code that appears to be abnormal
* }
*
* Continue to perform the following normal code
public static void Main (string[] args) {
Math1 math1 = new Math1 ();
try {
Math1.div (3, 0);
} catch (ArithmeticException e) {
E.printstacktrace ();
}catch (ArrayIndexOutOfBoundsException e) {
E.printstacktrace ();
}catch (Exception e) {//Note: Be sure to put the exception capture that contains the Exception back, or the exception that follows does not execute the parent class exception all exceptions can be received
E.printstacktrace ();
}
System.out.println ("Go on");
}
}
Class math1{
Int[] arr = {3,5};
public int div (int a, int b) {
System.out.println (arr[1]);
return a/b;
}
}
* try{
* Code that may appear abnormal
*}catch (Exception e) {//catch exception E is the exception to be caught
* Processing of code that appears to be abnormal
*}finally{
* Code that must be executed: role: For the release of resources: for example: Lock object in multi-thread, stream close, database shutdown, etc.
* }
*
*
*
* Continue to perform the following normal code
*
* try{
* Get Resources
*}finally{
* Release resources.
* Code that must be executed: role: For the release of resources: for example: Lock object in multi-thread, stream close, database shutdown, etc.
* }
public class Demo5 {
public static void Main (string[] args) {
Math2 math2 = new Math2 ();
try {
Math2.div (22, 0);
} catch (ArithmeticException e) {
E.printstacktrace ();
return;//let the current method end, finally the code can still execute
System.exit (0);//exit the program, finally the code will no longer execute
} finally {
Code that must be executed
System.out.println ("finally");
}
System.out.println ("Go on");
}
}
Class math2{
public int div (int a,int b)//2. Throw it here. New ArithmeticException ()
{
Int[] arr = {3,4};
System.out.println (arr[1]);
Return A/B;//1. Generate and throw new ArithmeticException ()
}
}
9. Custom Exceptions
Custom exception: Your own definition of the exception class, due to the exception inside the basic function of the exception, we generally write exception sub-class
*
* Why do I have a custom exception?
* A: The system does not define an exception that requires our own definition, we solve the problem that the system does not solve.
*
* For example: Order exception user Information exception divisor is negative
*
* Classification of exceptions:
* Compile exception: Thrown at compile-time, handled exception---all exceptions except RuntimeException
* All relevant work must be done by ourselves.
* Runtime exception: Thrown at run-time, handling exception--runtimeexception exception
* All the work we can do, no matter what
*
* How to handle Exceptions:
* 1. Declaration of exception
* After the exception declaration, the caller goes to process, the caller does not process, and continues to declare until it is handed over to the JVM
* 2.trycatch
* Implementation of Real exception handling
*
*
* Is it appropriate for someone to handle the exception?
* Who calls the method that may appear to be abnormal, who is responsible for handling the exception
*/
To customize a negative exception class:
Class Fushuexception extends exception{
Public Fushuexception () {
}
Public fushuexception (String message) {
Super (message);
}
}
public class Demo6 {
public static void Main (string[] args)
{
Math3 math3 = new Math3 ();
1.trycatch 2. Continuation of the Declaration
try {
Math3.div (3,-2);
} catch (Fushuexception e) {
E.printstacktrace ();
}
}
}
Class math3{
Declaring an exception, telling someone I might have an exception
public int div (int a,int b) throws Fushuexception
{
/*
*throw is the meaning of throwing
*throws is the meaning of declaring an exception
*/
if (b<0) {
throw new Fushuexception ("divisor is negative");//manually generate and throw a divisor to a negative number exception
}
return a/b;
}
}
Java Object-oriented:
1. Everything objects
2. Object-oriented and process-oriented differences:
Process-oriented is primarily a matter of describing a thing or a process of execution
Object-oriented programming is mainly the use of language to describe the real world in the existence of things, or laws. A language that is infinitely close to machine recognition.
3. Object:
Objects include properties and features
4. Class: A class is an abstract concept or collection of all objects, which is called a concrete instance of a class.
The class is abstract and the object is specific
5. Three main features of object-oriented:
Encapsulation, inheritance, polymorphism
6. Encapsulation: Hide the internal implementation details, provide public access to external methods;
Benefits: Code Designer angle, security, prevent code from being modified to destroy;
Caller angle, ignoring implementation details, focusing only on how the function is used.
Design: member variables as far as possible privatization (private) to provide public methods externally
Inheritance: Derive a new class from the current class that can obtain all (non-private) properties and methods of an existing class, and can add its own properties and methods. That is, the subclass inherits the parent class.
Polymorphism: Multiple forms of an object, based on inheritance
The polymorphism of an object: upward transformation and downward transformation
Polymorphism of methods: override of overloaded methods of methods
7.package Pack
Classify all classes for ease of management
Naming rules: Lowercase letters, usually four-level directories: COM. Name of the company. Module names
8. Construction method: What is a construction method? The construction method is a class in the initialized form, and the class itself comes with features.
When the JVM constructs a class, it constructs a default construction method.
Characteristics of the construction method:
Public, same as class name, no return value
Where is the use of the construction method?
Initializes the member variable.
The more constructs the method represents, the more forms and functions are powerful when the class is initialized.
Overloaded Concepts for construction methods (overload)
Overloading of construction methods, different parameter types, different number of parameters, and different order of parameters can make up overloads. is limited to this class only.
9. member methods of the class:
Concept of a method: A method is a channel in which a class receives external information.
Method specifically solves the business logic in a class.
Public return value method name (parameter list) {
return value;
}
10. Passing of method parameters: pass-through values or pass-through references
Method can pass only two types of parameter passing:
If a value is passed, it is a specific value.
If you are passing a reference, the address of the object passed
11.static is used to modify static methods, blocks of code, or variables, which can be called directly using the class name, without requiring the new object to be called
Static modified variable has only one address in memory
Usage scenario: Can be used only in tool classes, saving memory space
12.static is used to modify static methods, static code modules, static variables
If you use a method or variable modified by static, you can call it directly using the class name.
The new object is not required to be called.
The static modified variable has only one address in memory,
Usage scenario: Only available in the tool class, this method is frequently called, does save memory space
13. Inheritance of classes extends
What are the relationships between classes and classes?
The first is the Association (HAS-A) relationship (aggregation), and the second is the inheritance (is-a) relationship
If it is an association, it is the relationship of the class invocation class.
All objects inherit the object class, and Java is the characteristic of a single inheritance.
What are the benefits of a single inheritance? A bad place?
Subclasses can call methods of the parent class to reduce the redundancy of the code.
In terms of the parent class, inheritance can extend the functionality of the parent class (extended functionality)
If the subclass inherits from the parent class, the child class can call the public method of the parent class and cannot invoke the private method of the parent class
When a subclass invokes the parent class, the constructor method of the parent class is called
The 14.super keyword can only exist in subclasses, and methods that call the parent class primarily in a method of a subclass, including the constructor method, are generally written in the first line of the program
Super can call the construction method of the parent class in the constructor of the subclass Super ();
Overriding subclasses override the parent class's method of the same name, with the benefit of extending the method functionality of the subclass.
The Richter substitution principle, if Class B and Class C have the same properties and methods, you should extract the same properties and methods into Class A, and let Class B and Class C inherit Class A
15.final keywords
Represents the final. If a variable is modified with final, it is called a constant (cannot be changed or assigned).
If final modifies a method, the method cannot be overridden in a subclass.
If final modifies a class, then the class cannot be inherited.
16. Abstract keyword
Public abstract class Name {
}
What is the difference between an abstract class and a normal class? Abstract classes cannot be instantiated, that is, the new keyword cannot be used, and ordinary classes can be instantiated
If a class has an abstract method, then the class must be an abstract class, whereas an abstract class may not necessarily have an abstract method.the person chasing the kiteExperience, what is the purpose of defining an abstract class? Abstract concepts do not need to be actually done, but there are two scenarios used in practical applications:
1, the parent Class A function or method, requires the subclass to be forced to rewrite, then the method must be abstract, this class must be abstract class.
2, the parent class for a method, can not predict how the child class will be implemented, then the method must be abstract.
What does an abstract class do in an inherited diagram?
The role of the connecting link.
17. Interface interface
The concept of interface:
Interfaces are a standard, and all subclasses that implement the interface implement this standard.
An interface is also a contract in which all subclasses implementing the interface implement the terms of the contract.
An interface is also an identity feature, and all subclasses have that identity.
All the methods inside the interface are abstract and public.
All member variables in the interface are constants.
Can interfaces inherit? An interface can inherit an interface.
18. polymorphic
What is polymorphic? A thing in different conditions, the manifestation of many forms.
The use of polymorphic scenarios in development, mainly using the method's parameter passing, can pass the interface, can also pass abstract class
Instanceof to determine if there is a relationship between two classes
objects are transformed up and down:
Upward transformation: Unified Standard A parent class Cdef subclasses can be executed according to the criteria of the parent class, with the meaning of downward transformation: using a method unique to a subclass.

Analysis of exception in Java programming and its object-oriented thinking summary [figure]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.