Study number 20145336 "Java Program Design" 5th Week study summary textbook Learning content Summary
Using try, catch
All errors in the try, Catch:java are wrapped into objects, and you can try to execute the program (try) and catch (catch) to represent the wrong object and do some processing. Using the try, catch syntax, the JVM tries to execute the program code in the try block, and if an error occurs, the executor jumps off the point of the error, and then executes the program code in the catch if it matches the type declared in the catch bracket, whether it conforms to the type of the wrong object being thrown.
public class Average {
public static void Main (string[] args) {
Scanner Scanner = new Scanner (system.in);
Double sum = 0;
int count = 0;
int number;
while (true) {
Number = Scanner.nextint ();
if (number==0) {
Break
}
Sum+=number;
count++;
}
System.out.printf ("Average%.2f%n", sum/count);
}
}
After the wrong input, the system will handle it correctly.
Exception Inheritance Schema
The error is wrapped as an object inherited from Java.lang.Throwable, and he has two subclasses java.lang.Error with Java.lang.Exception.
On the syntax and inheritance architecture, if a method declaration throws an instance of Throwable or subclasses, as long as it is not part of the error or java.lang.RuntimeException or its subclass, it must be handled explicitly using the try, catch syntax. Or in the method with throws declaration of this method will throw an exception, otherwise it will fail to compile, the use of throws also improve the security of the code.
If a method declaration throws an instance of Thorwable or subclasses, as long as it is not an error or java.lang.RuntimeException, you must explicitly use the try, catch syntax plus a handle. Or use throws in the method to declare this method throws an exception, or the compilation fails
If you want to know the origin of the anomaly, and the stack propagation of the exception under the multiple method invocation, you can take advantage of the stack trace that the exception object collects automatically to get the relevant information, such as calling the Printstacktrace (), Getstacktrace () of the exception object. To be good at stack tracking, if the program code is not pocketed abnormal behavior, improper handling of the exception, or display incorrect information. In the case of throwing a throw exception, the exception's trace stack start point is still the source of the exception, not the place where the exception was thrown. If you want to make the exception stack start to throw out the exception, you can use Fillinstacktrace (), which reloads the exception stack, sets the starting point to throw the exception in place, and returns the Throwable object.
To catch or to throw
The exception is detected at compile time: The exception is compiled and fails if it is not processed (no throw or try). The exception is represented and the delegate can be processed. • Runtime exceptions (not detected at compile time): At compile time, no processing is required and the compiler does not check. This exception occurs, it is recommended not to process, let the program stop, the code needs to be corrected.
import java.io.*;
Import Java.util.Scanner;
PublicClass fileutil{
PublicStaticString ReadFile (String name) throws filenotfoundexception{
StringBuilder text=New StringBuilder ();
try{
Scannerconsole=New Scanner (New FileInputStream (name));
while (console.hasnext ()) {
text.append ( Console.nextline ())
. Append ( }
}catch (FileNotFoundException ex) {
Ex.printstacktrace ();
throw ex;
}
return text.tostring ();
}
}
Understanding Stack Traces
In the case of multiple method calls, the point of the exception may be in a method, if you want to know the origin of the exception, and the stack propagation of the exception under the multiple method invocation, you can use the stack trace to obtain the relevant information. The use method is to call the Printstacktrace () of the exception object directly. The stack trace information shows the exception type, the topmost is the source of the exception, the following is the order of the calling method, the program code line is corresponding to the original program source code, if you want to get the individual stack trace elements for processing, you can use Getstacktrace (), Without doing anything after catching an exception or doing inappropriate processing, this code can cause serious damage to application maintenance. When using throws to re-throw an exception, the exception's trace stack start point is still the source of the exception, not the place where the exception was thrown. If you want to make the exception stack start to throw out the exception, you can use the Fillinstacktrace () method, which reloads the exception stack, sets the starting point to the place where the exception is thrown, and returns the Throwable object.
Understanding the collection Architecture
Javase provides APIs to meet a variety of requirements, and the behavior of collecting objects is defined in the java.collection, which can both collect objects and expel objects. The behavior of the collection object, such as the Add () method of the new object, the Remove () method for removing the object, and so on, are all defined in java.util.Collection. Since the object can be collected, but also to be able to get the object, this is the behavior defined by java.lang.Iterable, it defines the iterable () method to return the Java.util.Iterator operand, you can get the collected objects. The collection interface has three sub-interfaces, list, set, and queue, respectively. If you want the index order of each object to be recorded at the time of collection and retrieve the object by index, You can use the Java.util.List interface, if you want to collect objects that do not repeat, have the behavior of the collection, you can use the Java.util.Set interface, if you want to collect objects in a queue, if the objects are collected to the end, when the object is obtained from the front end, you can use the Java.util.Queue interface, as If you want to join, remove, and so on both ends of the queue, you can use Java.util.Deque.
List with index
A list is a collection that collects objects and preserves the order of collected objects in an indexed manner, with one of the operations being java.util.ArrayList. ArrayList attribute: The array will be contiguous linear space in memory, and the speed is fast according to the index random access. There is a constructor that can specify the capacity. LinkedList features: If the collected objects are often subject to changes in the index.
Problems in teaching materials learning and the solving process
This week to study is the 8th, 9 chapters of knowledge, learning how to handle the exception of the code, the usual code will have some small problems, through the students and books to explain can be explained, through the knowledge of this chapter, my ability to deal with the code of abnormal error has improved a lot, I can also carry out even the correct handling when I meet the wrong band code in the future.
Is my code-hosting:
Learning progress Bar
|
lines of code (new | /Cumulative)
Blog volume ( | new/cumulative)
Learning time (new/cumulative) |
Important growth |
target |
5000 Line |
30 |
400 hours |
  |
first week |
150/150 |
1/1 |
15/15 |
  |
second week |
150/400 |
1/2 |
15/30 |
  |
third week |
150/550 |
1/3 |
20/50 |
  |
fourth week /td> |
150/700 |
1/4 |
16/56 |
|
fifth week |
150/850 |
1/5 |
19/65 |
Resources
- Java Learning Notes (8th Edition)
- Java Learning Note (8th Edition) Learning Guide
- ...
Java 5th Chapter Study Summary