Study Number: 201621123032 Java programming 10th Week of study summary

Source: Internet
Author: User
Tags stack trace throw exception

1: Summary of this week's study 1.1.: Summarize abnormal content in the way you like (mind map or other).

2: Written assignment 2.1.: Common anomalies--combined with title 7-1 answer 2.1.1: What are the usual anomalies and needs to be captured in the code you wrote previously? What should be avoided?

There are two types of exceptions:

    • Syntax error: can be modified according to the compiler's prompt;
    • Run-time error, common with illegalargumentexception,nullpointerexception,indexoutofboundsexception. No capture is required.

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

      Snapping is not required for runtimeexception and its subclasses, while other exceptions require snapping.
      RuntimeException: Array out-of-bounds exception, null pointer exception, 0 divisor exception.
      Non-runtimeexception exceptions are: Socket exception, IO exception, and so on.

2.2.: Handling exceptions makes your program more robust-topic 7-22.2.1: Experimental summary. And answer: How can you make your program more robust?

Summary: If the input is a non-integer string, then you need to output the exception information, and then re-enter. Therefore, if an exception is caught in a for-loop try-catch catch exception, it needs to be i--in the catch block, otherwise an error will occur.

In the case of a valid use exception, the exception type answers "What" is thrown, the exception stack trace answers "where" is thrown, the exception message answers "Why" will be thrown. There are three principles that can help you maximize the use of exceptions during debugging, and these three principles are:

    • Specifically: use multiple catch blocks to provide the user with explicit information to catch the exception.
    • Early throw: A rapid failure by throwing an exception immediately when an error is detected can effectively avoid unnecessary object construction or resource consumption
    • Delayed capture: Captures a program before it can handle an exception, often resulting in more severe errors and other exceptions.
      You will be able to help you track and eliminate defects, make your program more robust and more user-friendly.
2.3:throw and throws--title 7-3--reading integer.parsetint source code 2.3.1:integer.parsetint There is a large number of code to throw the exception at the outset, what is the benefit of this practice?


A null or decimal appears when using Integer.parsein (), and an exception java.lang.NumberFormatException:For input string is emitted. Parameters that can avoid passing in errors.

2.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?

When begin >= End,begin > 0 or End > arr.length, throws a IllegalArgumentException exception and prints the reason for the exception.

if(begin>=end) {            throw new IllegalArgumentException("begin:"+begin+" >= end:"+end);                    }        if(begin<0){            throw new IllegalArgumentException("begin:"+begin+" < 0");        }        if(end>arr.length){            throw new IllegalArgumentException("end:"+end+" > arr.length");        }

The reason for the exception to be passed to the caller.

2.4: Use exception to improve arrayintegerstack--topic 6-32.4.1: Combine with 6-3 code, answer the question of how to use throw exception to represent the program run-time error What is the benefit? What is the advantage of returning an error value more than a simple one?

For a member method, it is easy to understand the success of the execution, and if the execution fails the method of throwing the exception will directly reflect the reason of the failure than the return error value.

2.4.2: If the inner code of a method throws an exception of type runtimeexception, then does the method declaration should 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?

There is no requirement to use the throws keyword, declaring that the throws keyword can prompt the exception and re-execute the program so that the program stops running after an error occurs when the user is using it. Using the throws declaration to customize the information that throws the exception, you can tell the user in more detail what went wrong.

2.5: Function Questions-Multiple exception captures-topic 6-12.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?

If multiple exception handling is the same, you can use catch (Exception e), or, if different, the subclass precedes the parent class. such as the NumberFormatException, illegalargumentexception and exception three kinds of anomalies. NumberFormatException is a subclass of IllegalArgumentException, IllegalArgumentException is a subclass of exception, So you write the catch block in the order of NumberFormatException, IllegalArgumentException, exception.

2.5.2: If you can throw multiple exceptions in a try block, what do you need to be aware of using JAVA8 's multiple exception capture syntax?

Note the inheritance relationship between multiple exceptions, you should first capture the subclass and then capture the parent class.

2.6: Add exception handling for 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));//打印数组内容
2.6.1: Correct the code so that it can function properly. Note 1: There are several methods inside that can throw an exception. Note 2: To use finally to close the resource.
    byte[] content = null;    FileInputStream fis =null;    int bytesAvailabe;    try {        //FileInputStream fis =null;    fis = new FileInputStream("testfis.txt");    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 {            if(fis!=null)            fis.close();        } catch (FileNotFoundException e) {            System.out.println(e);        }        System.out.println(Arrays.toString(content));//打印数组内容    }

2.6.2: Use try-with-resources in Java7 to overwrite the above code to automatically close the resource. What are the benefits of this approach?
public static void main(String[] args) throws Exception {                byte[] content = null;    FileInputStream fis =null;    int bytesAvailabe;    try {        //FileInputStream fis =null;    fis = new FileInputStream("testfis.txt");    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));//打印数组内容    }            }

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

Member: Luojia Lin Junwei Wang Yaiyun

2.7.1: Who is the user of this system?

The user is divided into two types: the Librarian of the student (the borrower).

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

Function Module: Login module book information Management System book borrowing module book return module book query module

2.7.3: The main class design and class diagram of the system (available)

2.7.4: How are you going to store library information, address information, reader information, and more?

The current idea is to store it in files, but the specific implementation has not yet been thought of.

3: Code Cloud and PTA3.1: Code cloud codes Submission record

3.2:pta the complete picture of the problem set

3.3: Count the amount of code done this week

]
Week Time Total code Amount new increase in code volume Total Folder Add new Folder
1 all all one
2 520 406 6
3 1089 569 [ 6
5 1425 336 7
6 1819 394 2
7 2987 1168 8
+ 5437 2450
9 5695 258 + 5
6680 985 102 |
one 6935 255 111 9

Study Number: 201621123032 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.