I. Packaging
Background: When defining an attribute for users, attributes modified with public cannot restrict the value of attributes, so encapsulation is needed.
ublic class Student { private int age;//Set access permissions to make their values controllable public void setAge(int age) { if(age>1&&age<20){ this.age =age; }else{ System.out.println("Age invalid"); } } public int getAge() { return age; } }
At this point, we can limit the scope of user usage by defining the diagram in the class.
2. throw and throws
Background: When using encapsulation, when the user enters the wrong value, the corresponding error prompt and address should be popped up.
1. Tracking of Packaging Error Tips
We thought of using the method of prompting exceptions to track down:
At this point, we add:
public void setAge(int age) { if (age>1&&age<20) { this.age = age; }else { throw new NullPointerException("Invalid code"); } } public int getAge() { return age; }
At this point, throw is used to throw runtime exceptions in the method into the user interface
Exception in thread "main" java.lang.NullPointerException: Invalid code at www.jd.student.Student.setAge(Student.java:12) at www.jd.book.Book.main(Book.java:8)
2. Create new exceptions
Background: Above exceptions pop up the // null pointer code entered by ourselves, which obviously does not match the age. Next, we need to redefine exceptions through inheritance.
Runtime exception
public class AgeException extends RuntimeException { private static final long serialVersionUID = -8523414367897761594L; public AgeException(String message) { super(message); } }
Inherited Runtime Exception (runtime) method calls the method of output errors in the parent class by super. At this time, a new runtime exception class corresponding to the age is created. At this time, an ID needs to be generated automatically, which is not explained now.
public void setAge(int age) { if (age>1&&age<20) { this.age = age; }else { throw new AgeException("Invalid age"); } }
Running results:
Exception in thread "main" www.jd.exception.AgeException: Invalid age at www.jd.student.Student.setAge(Student.java:11) at www.jd.book.Book.main(Book.java:8)
At this point, it seems quite reasonable.
2. Abnormal during examination
Rewriting the parent class inherited from the above code to a non-runtime parent can successfully create the check-time exception class.
public class AgeException extends Exception { private static final long serialVersionUID = -8523414367897761594L; public AgeException(String message) { super(message); } }
3. throw and throws
Background: When errors are detected in the encapsulation method, it is necessary to throw the error information to the user's execution interface so that the user can get the corresponding information. At this time, throw and throws are needed to throw.
The position of throwing
Throw to yourself: When using throw, you can throw objects in this class through try {catch(){throw in this class;
public void setAge(int age) { if (age>1&&age<20) { this.age = age; }else { try{ throw new AgeException("Invalid age"); }catch(AgeException e) { e.printStackTrace(); } } }
This is where the code in try catch {} is not executed when checked in the user class (instructions are only thrown to this class)
Throwing to an external class (as long as exceptions are not thrown to this class, they are thrown to an external class)
Runtime exception
In case the exception is a runtime exception, throw the exception object directly to the user interface (no display processing is required)
2. Abnormal during examination
When checking for abnormal behavior, throws (before parentheses after methods) must be used to throw out the class name.
public void setAge(int age)throws AgeException { if (age>1&&age<20) { this.age = age; }else { throw new AgeException("Invalid age"); } }
3. Code block:
Check-time exceptions cannot be thrown in code blocks
Failure to establish the method shown in the figure
Summary:
throws must appear when throwing checks, which throw exception classes
throw throws an exception method.