Study Number: 201521123116 Java programming Nineth Week of study summary

Source: Internet
Author: User

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 work problem set异常

1. Common Exceptions

Topic 5-1

1.1 Your Submission Results (Advent number)

1.2 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?

Some anomalies, such as runtimeexception in the program is completely avoidable, such as the array of the cross-border, as long as the program to make a judgment, if the access to the array element subscript and the length of the group to make a comparison will know will not cross. This exception is detected by the system and belongs to the unchecked Exception, so you do not need to use try catch for exception capture. Therefore, we need to use the if statement to determine whether the code is empty, to avoid the occurrence of nullpointerexception;

1.3 What kind of exception requires the user to be sure to use capture processing??
Previously mentioned exceptions that are directly inherited from exception require capture processing.

Q2. Handling exceptions to make your program more robust
Topic 5-2
2.1 Your Submission results (Advent number)

2.2 Experimental Summary

Answer:
Topic 2 requires the use of abnormal mechanisms to handle the exception input, in fact, compared to the topic 1 is relatively simple. First instantiate an array, then loop through the data, and use Try-catch to implement a non-integer string hint exception when entered.

Q3.throw and throws
Topic 5-3
3.1 Your Submission results (Advent number)

3.2 Read the Integer.parsetint source code, combined with 3.1 what information does it need to pass to the caller when throwing an exception?

Import Java.util.Arrays;
Import Java.util.Scanner;

Class arrayutils{
public static double Findmax (double[] arr,int begin, int end) throws illegalargumentexception{
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");
}
Double max= arr[begin+1];
for (int i=begin+1;i<end;i++) {

if (Arr[begin+1]<arr[i]) {
Double t=arr[begin+1];
Arr[begin+1]=arr[i];
arr[i]=t;
MAX=ARR[BEGIN+1];
}


}
return Max;
}


}


public class Main {

public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
int N=sc.nextint ();
Double[] Arr=new Double[n];

for (int i=0;i<arr.length;i++) {
Double s=sc.nextdouble ();
Arr[i]=s;
}
while (Sc.hasnextint ()) {
int Begin=sc.nextint ();
int End=sc.nextint ();
try{
System.out.println (Arrayutils.findmax (arr,begin,end));
}catch (Exception e) {
System.out.println (e);
}

}
Sc.close ();
try {
System.out.println (ArrayUtils.class.getDeclaredMethod ("Findmax", Double[].class,int.class,int.class));
} catch (Exception E1) {
}
}

}

From the Integer.parseint () source code, you can see that depending on the situation throws an exception, you can throw specific exception information according to different circumstances, so that callers understand the cause of the exception. In pta5-3, the size of the [Begin,end] range, you need to handle the value of the begin,end, in case of an exception, print different exception information for different exceptions can let the caller understand is the presence of begin>=end,begin< 0, or a end>arr.length exception.

4. Function Title 4-1 (capture of a variety of exceptions) 4.1 Your submission results (number of occurrences)

3.2 In a try block if you can throw multiple exceptions, what do you need to be aware of when capturing?

A: When you throw multiple exceptions, be aware that the subclass exception is written in front of the parent exception when you write the catch. If the parent class is in front of the exception. You do not perform a capture of the subclass exception, and the compilation error occurs. For example:

catch (numberformatexception E1) {

System.out. println (E1); //Execute, if captured, then output numberformatexception;

}

catch (illegalargumentexception E2) {

System.out. println (E2); //Execute, if captured, then output illegalargumentexception;

}

catch (Exception e3) {

System.out. println (E3); //Execute, if captured, then output exception;

}

When changing the order of three exceptions:

catch (Exception e3) {

System.out. println (E3); //Execute, if captured, then output exception;

}

catch (numberformatexception E1) {

System.out. println (E1); //Do not execute;

}

catch (illegalargumentexception E2) {

System.out. println (E2); //Do not execute;

}

A compilation error has occurred.

5. Add exception handling to the following code

byte[] content = null;
FileInputStream fis = new FileInputStream ("Testfis.txt");
int bytesavailabe = fis.available ();//Gets the number of bytes available for the file
if (bytesavailabe>0) {
Content = new byte[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

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

Ans:
The problem of the code does not work properly, that is, it will error, then want it to run normally, you need to find out where the wrong, so we directly copy and paste the title of the code to get the following error:

Error shows abnormal filenotfoundexception and ioexception are not processed.
Using the JDK documentation to query the FileNotFoundException and IOException classes, the discovery was surprising:

The FileNotFoundException class is a subclass of the IOException class, which means that throws IOException in the main function throws all exceptions that are not handled.
Therefore, the procedure does throws processing, obtains

Putting the code in eclipse suggests two solutions: one is to throw a ioexception directly, and the other is the code that Tyr catch might go wrong; Here I write only the first, close the resources I will directly file.close

Q6. Key assessment: Using exceptions to improve your shopping cart system
Give at least two examples of how you can use the exception handling mechanism to make your program more robust.
The description consists of 2 parts: 1. The problem description (where the exception is encountered). 2. Solution (Key Code)

3. Code submission record on code cloud

Topic Set: Exceptions

Study Number: 201521123116 Java programming Nineth 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.