Java basic notes-exception summary, package, and java basic notes Summary

Source: Internet
Author: User

Java basic notes-exception summary, package, and java basic notes Summary

Exception: it is a description of the problem. It encapsulates the problem objects,

Abnormal system:

Throwable:

1. Error

2. Exception

1. RuntimeException

Characteristics of the exception system:

All classes and created objects in the exception system are throttled.
It can be operated by the throw and throws keywords. Only the exception system has this feature.

 

Difference between throw and throws:

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 without trycatch, throws must be declared on the function; otherwise, compilation fails.
Except for RuntimeException, if a RuntimeExcpetion exception is thrown in the function, the function does not need to be declared.

 

Exception Handling statement:
Try
{
Code to be detected;
}
Catch ()
{
Code for handling exceptions;
}
Finally
{
Code that will be executed;
}

Finally: the code that will be executed. It usually defines the code used to close the resource.

Special case: when the System. exit (0); statement is executed before the finally statement, the finally statement will not be executed.

 

Custom exception:

Define class inheritance Exception or RuntimeException

1. To make the custom class disposable.

2. Let this class have a common method for abnormal operations.

To customize the information about an exception, you can use the defined function of the parent class to pass the exception information to the constructor of the parent class.

Class xxxException extends Exception

{

XxxException (String message)

{

Super (message );

}

}

Custom exception: encapsulate special problems in the program based on the object-oriented idea in Java.

When the child parent class is overwritten:
1. The exception thrown by the subclass must be a subclass or subset of the exception of the parent class.
2. If the parent class or interface does not throw an exception when it is overwritten by a subclass, it can only be processed by trycatch instead of throw/throws.

1/* 2 evaluate the circle and the rectangle area 3 */4 class NoValueException extends RuntimeException // Exception 5 {6 // private String message; 7 NoValueException (String message) 8 {9 super (message); 10} 11} 12 13/* 14 abstract class Shape 15 {16 abstract void getArea (); 17} 18 19 class rec extends Shape 20 */21 // The interface is also an abstract Method 22 interface Shape 23 {24 void getArea (); 25} 26 class Rec implements Shape 27 {28 private int len, wid; 29 Rec (int len, int wid) // throws NoValueException 30 {31 if (len <= 0 | wid <= 0) 32 throw new NoValueException ("len, wid value is invalid "); 33/* 34 {35 System. out. println ("len, wid value illegal"); 36} 37 */38 39 this. len = len; 40 this. wid = wid; 41 42} 43 public void getArea () 44 {45 System. out. println ("the rectangle area is:" + len * wid); 46} 47} 48 class Circle implements Shape 49 {50 private double radius; 51 public static final double PI = 3.14; 52 Circle (double radius) throws NoValueException 53 {54 if (radius <= 0) 55 throw new NoValueException ("an invalid value occurred"); 56 57 this. radius = radius; 58} 59 public void getArea () 60 {61 System. out. println ("the area of the circle is:" + radius * PI); 62} 63 64} 65 66 class predictiontest2 67 {68 public static void main (String args []) 69 {70/* 71 // inherits Exception. 72 try 73 {74 Rec r = new Rec (-3,4); 75 r. getArea (); 76} 77 catch (NoValueException e) 78 {79 System. out. println (e. toString (); 80} 81 */82 // inherits RuntimeException, you do not need to perform the try operation. the program stops when an exception occurs. 83 Rec r = new Rec (3, 4); 84 r. getArea (); 85 86 87 try 88 {89 Circle c = new Circle (-3); 90 c. getArea (); 91} 92 catch (NoValueException e) 93 {94 System. out. println (e. toString (); 95} 96 97 98 99 100} 101 102}

 

Package: package

Purpose: Separate the source file from the class file.

Format:

Package name; // The definition statement must be defined in the first line of the Code. (top)

With the package name, the full name of the class name is: package name. Class Name (this class should be on the package)

After the classes in the package inherit from each other, you can access the properties and Methods Modified by protected. There are two types of permissions available between the package and the package: public and protected.

The following figure shows whether the access between the four permissions is allowed:

Public protected default (default permission) private

In the same class, OK

In the same package, OK No

Subclass OK No

OK No in different packages

Note: In a java file, there cannot be two or more public classes or interfaces.

 

Import: import keyword.

Description: Used to simplify the writing of the class name. After the import, the class name can be abbreviated directly, and it does not need to be written in the form of package name. Class Name.

 

Object-oriented seems to have finally finished learning, but I still don't know much about it, but I can only continue to look at the object-oriented knowledge while I am studying it myself.

 

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.