201621123014 Java programming Tenth week of study summary

Source: Internet
Author: User
Tags finally block

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?

A: The most common exception is nullpointerexception, which does not need to be captured because it does not directly inherit the exception, but avoids the object that does not call NULL.

1.2 What kind of exception requires the user to be sure to use capture processing?

Answer: Directly inherit the exception exception.

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?

A: Set the exception, create an array, according to their input n to the input data n times, when the input content does not meet the required type, catch and prompt for this exception, and this time the input is not counted in the number of inputs, the last print array.

3. Throw and throws

Topic 7-3
Read 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:

You can filter the input information, throw the non-conforming input, and prevent the code from running without error.

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?

Answer: The location of the exception, the type of exception, and the workaround.

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?

A: Throwing an exception can more visually show where the program is running without affecting the rest of the program, and you can continue to run the code in the catch to find a workaround.

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?

A: Not required, RuntimeException type of exception program will be processed; Throwing an exception using throws can catch the exception outside of the program.

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?

A: To set multiple catch-catch exceptions, it is best not to catch the exception in front of the parent class, or catch the wrong 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?

A: You should set different catch snaps for multiple exceptions, and if the exception has an inheritance relationship, the parent will be snapped later, or it might cause a catch error.

6. Add exception handling to the following code
bytenullnew FileInputStream("testfis.txt");int bytesAvailabe = fis.available();//获得该文件可用的字节数if(bytesAvailabe>0){    newbyte[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:

byte[] content =NULL; FileInputStream FIS =NULL;Try{FIS =NewFileInputStream ("Testfis.txt");intBytesavailabe = fis.available();//Get the number of bytes available for this file            if(bytesavailabe>0) {content =New byte[Bytesavailabe];//Create an array that can hold the file sizeFis.Read(content);//Read the contents of the file into an array}        }Catch(FileNotFoundException e) {System. out.println("file xxx not found, please re-enter the filename"); System. out.println(e); }Catch(Exception e) {System. out.println("failed to open or read the file!"); System. out.println(e); }finally{Try{System. out.println("Close file ing"); Fis.Close(); }Catch(Exception e) {System. out.println("Failed to close the file!" "); System. out.println(e); }} System. out.println(Arrays.toString(content));//print array contents
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?

A: To put the action that must be performed in the finally block, note that the action in the try may have an exception, if the error occurs, the code in the try does not execute, in the finally if there is a close try in the resource can be an error.

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:

    try (FileInputStream fis =  new FileInputStream("testfis.txt")) {                  new FileInputStream("testfis.txt");                bytesAvailabe = fis.available();                if(bytesAvailabe>0){                    newbyte[bytesAvailabe];                    fis.read(content);            catch (Exception e) {                  e.printStackTrace();              }

Simple and fast, try-with-resources shuts down resources regardless of whether a try can be executed, prevents errors, and reduces the amount of code.

7. Object-oriented design job-Library management system (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?

Answer: Users, administrators

7.2 The main class design and class diagram of the system (available

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.

Week Time Total code Amount New Code Volume total number of files number of new files
1 0 0 0 0
2 0 0 0 0
3 313 313 9 9
4 641 328 20 11
5 84W 305 25 5
6 1451 505 32 7
7 1969 518 35 3
8 2284 315 43 8
9 2682 344 47 4
10 2938 256 55 8

201621123014 Java programming Tenth 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.