201621123013 Java programming 10th Week of study summary

Source: Internet
Author: User

1. Study summary 1.1 This week summarize anomalies in the way you like (mind mapping or other).

2. Written work this time PTA work problem set abnormal 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?

For:
1. Exceptions that occur frequently in your own code: When you access an array: There are ArrayIndexOutOfBoundsException exceptions that occur when you force a conversion type:ClassCastException

2, do not need to capture. The last exception was a RuntimeException subclass exception, and it was an Unchecked Exception exception (not necessarily captured).

3, we write the code should pay more attention to such as array boundaries, as well as the problem of strong turn type.

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

CheckedExceptionA: The exception that belongs to requires that the user must use 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?

For:
try{ String b = sc.next(); a[i]=Integer.parseInt(b); i++; }catch(Exception e){ System.out.println(e); }

Put a statement that may produce an exception into a try, and we enter a non-integral string with an exception, and catch catches the exception. We use the exception handling try-catch mechanism to make our programs more robust.

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?

A: By reading the source code, you can see that the exception is thrown, which allows the NumberFormatException user to quickly know the cause of the exception, without having to think about what kind of exception we have.

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?

A: The exception we throw in the subject is IllegalArgumentException , according to the topic understanding, in these three cases throws an exception, respectively:

1, when Begin>=end.

2, begin < 0 o'clock.

3, when End>arr.length.

Explain the causes of each exception in the exception, and then find a way to resolve the exception.

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?

A: The advantage: Using the way that throws an exception throws an exception when an error occurs, terminates the program, rather than simply returning the error value, it allows us to better understand its error message, so that we can modify the code.

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: You do not have to use throws keywords, because RuntimeException types of exceptions are not necessarily captured.

The benefit: If you use the throws keyword to declare the exception thrown by this method, we don't have to write about it so we try-catch can terminate the program directly.

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?

A: Note that in the summary there is a write, a try block if you can throw a variety of exceptions, and there may be an inheritance between exceptions, the subclass exception to be placed in front of 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?

A: When using Java8 's multiple exception capture syntax, it is important to note that the child class exception captures must precede any parent class.

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!

Correct:

byte[] content = null;try{    FileInputStream fis = new FileInputStream("testfis.txt");    int bytesAvailabe = fis.available();//获得该文件可用的字节数    if(bytesAvailabe>0){        content = new byte[bytesAvailabe];//创建可容纳文件大小的数组        fis.read(content);//将文件内容读入数组    }}catch(FileNotFoundException e| {    System.out.println(e);    }catch(IOException e){    System.out.println(e);}finally{        try {                                  fis.close();         }          }catch(Exception e){            System.out.println(e);        }     }}System.out.println(Arrays.toString(content));//打印数组内容
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: We know that the statements in the finally block are executed regardless of whether or not the exception is caught, because we definitely have to close the resource, but it is also possible to encounter an exception and catch it when doing this.

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:

public class Test {    public static void main(String[] args) {        byte[] content = null;        try(FileInputStream fis = new FileInputStream("testfis.txt")){            int bytesAvailabe = fis.available();//获得该文件可用的字节数            if(bytesAvailabe>0){                content = new byte[bytesAvailabe];//创建可容纳文件大小的数组                fis.read(content);//将文件内容读入数组            }        }catch(Exception e){            System.out.println(e);        }        System.out.println(Arrays.toString(content));    }}

Benefit: Using the try-with-resource statement, the close function is automatically called, closing the resource, which is more concise and convenient than the previous code.

7. Object-oriented design job-Library management system (group complete, no more than 3 students per group) login lib.jmu.edu.cn, 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? 7.2 Main function modules (not too many) and the owner of each module. Next week everyone is going to submit their own module code and run the video. 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: Exception 3.1. Codes Cloud code submission is recorded in the Code cloud Project, select "Statistics-commits history-set time period", then search and

3.2 PTA Problem Set completion diagram needs to have two pictures (1. Ranking chart. 2.PTA Submission list diagram)

3.3 Statistics The amount of code completed this week requires that the weekly code statistics be fused into a single table. 526 /tbody>
weeks Total code amount new Code volume Total Files number of new files
2 0 0 0 0
3 235 235 6 6
5 498 263 even 7
6 711 213 6
7 1237 one
8 1670 433 43
9 2352 682 18
2940 588 +

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