201621123057 Java programming 10th Week of study summary

Source: Internet
Author: User
Tags finally block set time try catch

1. Summary of this week's study

1.1 Summarize abnormal content in the way you like (mind map or other)

2. Written work

This PTA job problem set anomaly

1. Common exceptions

Combined Topic 7-1 Answer
1.1 What exceptions do you often have in code that you have written before, and what do you need to capture (why)? What should be avoided?
For:
Input exception ( NumberFormatException ClassCastException ) unchecked exception;

Array out of bounds ( ArrayIndexOutOfBoundsException ) unchecked exception;

The way to avoid this is to enter it correctly.

1.2 What kind of exception requires the user to be sure to use capture processing?
For:
RuntimeException and its subclasses: no Try-catch required;
Exception and the subclasses of direct inheritance exception: must be try-catch processed;

2. Handle exceptions to make your program more robust

Topic 7-2
2.1 Experimental summary. And answer: How can you make your program more robust?
For:
This problem needs to be noted that the array subscript should be reduced by one after the exception is taken and captured;
Make the program more robust, first in the event of an exception can be self-processing so that the program can continue to run normally, followed by telling the type of the exception, remind them to avoid the next error.

3. Throw and throws

Topic 7-3
Read the Integer.parsetInt source code
3.1 Integer.parsetint There is a lot of code thrown out of the exception at the outset, what is the benefit of this practice?
For:
The code program is more robust by throwing a large number of exceptions to resolve the various errors that may occur.

3.2 What information do you usually need to pass to the caller when you write your own program with 3.1 and analyze your own method of throwing an exception?
For:

    • Type of error (IllegalArgumentException)
    • The specific cause of the error (Begin and end size judgment)
    • Remind users how to solve and avoid making mistakes again
4. Improve Arrayintegerstack with exception

Topic 6-3
4.1 In combination with 6-3 code, how does it benefit to use an exception-throwing method to represent a program run-time error? What is the advantage of returning an error value more than a simple one?
For:
The use of throwing exceptions can be made when the user knows the error, and the error type is also clear, this is a simple return error value can not do (previously simply return a "1" to indicate an error, it is not clear).

4.2 If the inner code of a method throws an exception of type RuntimeException, does the method declaration use the throws keyword, and if the exception that is thrown by the method is declared using the throws keyword, what benefit can it bring us?
For:
Exceptions of type runtimeexception belong to unchecked Exception, which can be declared without the use of the throws keyword;
An exception of type runtimeexception is the exception that occurs when a virtual machine is run, and it is produced more frequently, and it has too much impact on program readability and operational efficiency if it is to be handled. If you traverse an array, the efficiency is greatly reduced in order to handle the cross-border situation, plus the try catch block.

5. Function questions-capture of multiple exceptions

Topic 6-1
5.1 Combined with 6-1 code, answer: If you can throw multiple exceptions in a try block, and there may be an inheritance relationship between exceptions, what do you need to be aware of when capturing?
For:
It is important to note that subclass exceptions must precede the parent class exception.

5.2 If you can throw multiple exceptions in a try block, what do you need to be aware of when using Java8 's multiple exception capture syntax?
For:
Note: Exceptions in a catch block must not have an inheritance relationship

6. Add exception handling to the following code
byte[] content = null;FileInputStream fis = new FileInputStream("testfis.txt");int bytesAvailabe = fis.available();//获得该文件可用的字节数if(bytesAvailabe>0){    content = new byte[bytesAvailabe];//创建可容纳文件大小的数组    fis.read(content);//将文件内容读入数组}System.out.println(Arrays.toString(content));//打印数组内容

6.1 Correct the code and add the following functions. When the file cannot be found, you need to prompt the user not to find the file xxx, retype the file name, and then try to open it again. If it is another exception, prompt to open or read the file failed!.
Note 1: There are several methods inside that can throw an exception.
Feature 2: You need to add a finally close file. Regardless of whether the above code produces an exception, always be prompted to close the file ing. If closing the file fails, the prompt to close the file failed!

For:

Note: Close may also produce a null pointer exception, so:

6.2 Combining the title set 6-2 code, what kind of action to put in the finally block? Why? What do I need to be aware of when closing resources using finally?
For:
6-2 Place Close () (freeing the resource) in the finally block, and finally the statement block will be executed in any case, so it is generally free of resources inside;
To close a resource using finally: Close may also produce a null pointer exception, so it is also try-catch to be processed.

6.3 Use try-with-resources in Java7 to rewrite the above code to automatically close the resource. What are the benefits of this approach?
For:

In this way, you do not need to write a finally statement block to release resources.

7. Object-oriented design job (group completion, no more than 3 students per group)

Log in to lib.jmu.edu.cn to search for books. Then log in to the library information system to view my library. If you want to implement a book borrowing system, try using object-oriented modeling.
7.1 Who is the user of this system?

    • User
    • Administrator

7.2 Main function modules (not too many).

    • User
      • Registered
      • Login
      • Search
      • Borrowing
      • Return
    • Administrator
      • Login
      • Add to
      • Delete

7.3 Main class design and class diagram of the system (available)

7.4 How you plan to store library information, address information, reader information, and more.

3. Code Cloud and PTA

Topic Set: Exceptions

3.1. Code Cloud codes Submission record
In the Code cloud Project, select statistics-commits history-set time period, and then search for and

3.2 PTA Problem set complete situation diagram

Two graphs are required (1. Ranking chart. 2.PTA Submission list diagram)

3.3 Count the amount of code completed this week

The weekly code statistics need to be fused into a single table.

201621123057 Java programming 10th Week of study summary

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.