201621123021 Java programming Tenth week of study summary

Source: Internet
Author: User
Tags finally block throw exception

1. Summary of this week's study

2. Written assignment 1. Common exception Combination Topic 7-1 Answer 1.1 What anomalies do you often have in your previously written code and need to be captured (why)? What should be avoided?
    • I used to write code is often encountered such as the topic of the array out of bounds, null pointers, strong turn failure, number format error, etc., I think these phenomena need to be captured.
    • To avoid these phenomena, you can use the Try-catch statement to catch exceptions, and then specifically request the paragraph to be processed specifically.
1.2 What kind of exception requires the user to be sure to use capture processing?
    • When we encounter checked exception, we need to capture it, such as file class files is checked exception need to capture processing.
2. Handling exceptions to make your program more robust The topic 7-22.1 experiment Summary. And answer: How can you make your program more robust?
    • In order to make the program more robust, it is easy to use the catch statement when the INPUT element is converted to an integer failure, use catch to catch the error, and then re-enter the element.
3. Throw and throws title 7-3 read Integer.parsetint source code 3.1 Integer.parsetint There is a lot of code to throw the exception at the beginning, what is the benefit of this practice?
    • Interger.parseint the beginning of the exception can prevent a variety of anomalies, so that the function is not necessary to call, can save memory and CPU resource consumption, prevent the waste of resources, save code run time.
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?
    • The exception thrown by the subject is to tell the caller that this function, Findmax is to use the array subscript and the array subscript must not be out of bounds, and this function starts subscript to be less than the end subscript otherwise the function throws a logic error.
4. Use exceptions to improve Arrayintegerstack title 6-34.1 combined with 6-3 code, answer the question of how to use throw exception to represent the program run error what is the benefit? What is the advantage of returning an error value more than a simple one?
    • It is more robust to use throwing exceptions to give the program exceptions to the caller for processing.
    • Throwing an exception makes it easier for the caller to correct the error than simply returning the error value.
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?
    • RuntimeException can understand that being a run-time exception is an error and the compiler will handle the error on its own.
    • If we use the throws method to throw an exception, this error can be thrown to the previous caller, making the program more flexible.
5. Function questions-Multiple exception capture topic 6-15.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?
    • Be aware of the exception inheritance relationships, note the level between them, and especially note that exception is the parent class for all exceptions, which must be placed at the end of the capture.
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?
    • Note that when catching exceptions, the various inheritance relationships of exception classes, whose inheritance illustrates the more specific, above and below captures from concrete to generalized.
6. Add exception handling to the following code
byte[] content =NULL; FileInputStream FIS=NewFileInputStream ("Testfis.txt");intBytesavailabe = Fis.available ();//get the number of bytes available for this fileif(bytesavailabe>0) {content=New byte[Bytesavailabe];//create an array that can hold the file sizeFis.read (content);//reading the contents of a file into an array}system. out. println (arrays.tostring (content));//Print Array Contents

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!.

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?
    • According to the set 6-2 code, we need to use the Try-catch statement to close the resource when we are using finally to close it, because this is the only way to determine if the exception is thrown when IOException needs to be left to the caller to resolve.
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?
    • Try-with-resources is a try-catch extension of the expression, in particular, to create an output stream object, the target assigned to it, Try-catch statement execution at the end of the resource is automatically closed, so that this can simplify the structure of the Code, At the same time, we avoid forgetting to close the file.
7. Object-oriented design job-Library Management system 7.1 who is the user of this system?
    • Librarian, the person who consulted.
7.2 Main function modules (not too many) and the owner of each module.
principal Module
Zheng Yupo Search for books, borrow a book
Jianheng Login, View information
Jiang Zholin Register, return the book
7.3 Main class design and class diagram of the system

7.4 How you plan to store library information, address information, reader information, and more.
    • I am prepared to use a file stream method to store book information, solve information and so on.
3. Code cloud and PTA3.1. Codes Cloud Code submission record

3.2 PTA Problem set complete situation diagram

3.3 Count the amount of code completed this week
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 0 0 0 0
4 437 437 7 7
5 905 468 13 6
6 1294 389 21st 8
7 1678 384 30 9
8 2184 40W 42 12
9 2678 494 56 14
10 3126 448 74 18

201621123021 Java programming Tenth week of study summary

Related Article

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.