201621123040 Java programming Tenth week of study summary

Source: Internet
Author: User
Tags throw exception

1. Summary of this week's study

2. Written assignments 2.1 Common Exceptions 2.1.1 What anomalies are often present in the code you have written previously, and need to be captured (why)? What should be avoided?
    • Arithmetic exception ArithmeticException (the case of a divisor of 0)
    • Type cast exception (in the case of a cast that ClassCastException cannot be converted during a type cast)
    • Array out-of-bounds exception ArrayIndexOutBoundsException (accesses an illegal element in an array)
    • The above-mentioned anomalies are runtimeexception subclasses, not necessarily captured;
    • Workaround: The case of a divisor of 0 is analyzed separately; consider whether the type conversion process is appropriate, if not appropriate, you can choose not to use this method of type conversion; When accessing an array element, using a For loop, 数组.length() accessing within the scope, or jumping out of the loop
2.1.2 What kind of exception is required for users to be sure to use capture processing?
    • The runtimeexception exception is a subclass of the exception exception, and the inherited RuntimeException exception is not necessarily captured, and any exception that inherits directly from exception needs to be captured and processed
2.2 Handling exceptions makes your program more robust 2.2.1 Experimental summary. And answer: How can you make your program more robust?
    • The subject according to test instructions ideas can be, in the Try-catch module, in the encounter character type conversion to an integer failure, re-enter the use i-- ; last print Arrays.toString()

    • Try to have Try-catch module in the method, avoid running the times wrong
    • When using the Try-catch module, use the finally statement as much as possible to release resources
    • At the time of multiple exception captures, it is recommended to add exception exceptions to the exception captures that are not considered
    • For the For loop for (int i=o;i<list.size (); i++) is obviously not as robust as for (int i=o;list!=null&&i<list.size (); i++)

Resources

2.3throw and throws2.3.1 Integer.parsetIntWhat is the benefit of having a large number of code throwing exceptions at the outset?

    • Let the compiler know that they have encountered in the transformation process of the abnormal problems, and to avoid anomalies

      if==null) s为空if<Character.MIN_RADIX)if>Character.MAX_RADIX)超限
2.3.2 combined with its own program and 3.1, the analysis of the method you write to throw an exception is generally required to pass to the caller what information?
    • begin<endThrow IllegalArgumentException exception
      begin<0Throw IllegalArgumentException exception
      end>arr.lengthThrow IllegalArgumentException exception

2.4 Using exception improvement ArrayIntegerStack2.4.1 combined 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?
    • Throws an exception, can be regarded as a breakpoint, easy to modify the error code of this section, more targeted; simply returning the error value is relatively less rapid and straightforward to find the wrong point, time-consuming
When should I use 2.4.2? throwKey word?
    • The Throw keyword is usually in the method body and throws an exception object of a class, and the throw statement can only throw an instance object of class Throwable or its subclasses, and the program will no longer execute subsequent statements when executing to the throw statement If you throw a runtimeexception or error, you can optionally handle the exception
2.4.3 If the inner code of a method throws an exception of type runtimeexception, then 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?
    • The RuntimeException throws keyword is not enforced when a type exception is thrown; If you use the throws keyword to declare an exception that is thrown by the method, you can break the run and find the part where the error is.
2.5 Function Questions-Multiple exception captures 2.5.1 in combination 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?
    • Because there may be an inheritance relationship between exceptions, it is necessary to catch the subclass exception before capturing the parent exception;
      The subject should be captured, captured NumberFormatException IllegalArgumentException , and finally captured Exception ( NumberFormatException the IllegalArgumentException IllegalArgumentException subclass Exception that is; the subclass of the Class)

2.5. If you can throw multiple exceptions in 21 try blocks, what do you need to be aware of when using Java8 's multiple exception capture syntax?
    • For each exception, the corresponding catch block should be written separately, and the subclass exception should be written before the parent class exception;
      If the action of the corresponding exception is the same, you can use the parent class exception to represent all exceptions
2.6 Add exception handling for the following code
Byte[] Content= NULL;FileInputStream FIS= New FileInputStream("Testfis.txt");int Bytesavailabe= FIS.available();//Get the number of bytes available for this fileif(Bytesavailabe>0){Content= NewByte[bytesavailabe];//Create an array that can hold the file size    FIS.Read(content);//Read the contents of the file into an array}System. out.println(Arrays.toString(content));//print array contents
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.

2.6.2 ending handling exceptions and using finally close resources what do you need to be aware of?
    • try-catch-finallyStatement, the finally contents of the statement must be executed, but there finally may be exceptions in it, so nesting in finally is used when necessarytry-catch
2.6.3 uses 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 the close function is automatically called when the resource is closed, greatly simplifying the number and distribution of the occurrence of the exception

2.7 Object-oriented design jobs (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.

2.7.1 who is the user of this system?
    • Students, teachers and staff, backstage management
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.
    • Students, teachers and staff:
    • Account Login
      Book Search
      Book borrowing
      Book return
      Check account information
      Exit system
    • Management staff:
    • Account Information Management
      Book Search

    • The initial goal is to realize the function of the student and teacher staff first.
    • Member: Lin Junwei (responsible for account login and book search)
      Wang Yaiyun (responsible for book lending and book Return)
      Luojia (Responsible for checking account information and exiting the system)

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

2.7.4 how you plan to store library information, address information, reader information, etc.
    • Think about using dynamic arrays to store relevant information, but once the amount of data is huge, it is inconvenient to manipulate; You can use files (read-in and read-out) related information
3. Code cloud and PTA3.1 codes to submit records

3.2PTA Problem Set Completion chart

3.3 Count the amount of code completed this week of
Weekly total code volume New Code volume Total number new files
First week 189 189 10 10
Second week 304 304 9 9
Third week 557 557 10 10
Week Four 621 621 8 8
Week Five 326 326 7 7
Week Six 972 972 12 12
Seventh Week 338 338 4 4
Eighth Week 379 379 6 6
Nineth Week 492 492 5 5
Tenth Week 473 473 6 6

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