1, Multiple choice questions
1.C
Parsing: for functions under void, you can use "return;" to indicate the end, but not "return i", because the data is void,
Try catch finally: execute correctly: try - > finally, unless try contains System.exit(0) to force exit. Error execution: try - > catch - > finally, the same for finally
2.C
Resolution: generally, no matter whether the try catch statement is abnormal or not, the final statement will execute finally
3.AC
Parsing: throws is used to declare an exception, which is used in method declaration; throws is used to throw an exception, which is used inside the method body
4.BC
Resolution: A:Exception is the parent of all exception classes, not the running exception. SexException does not exist
B:NullPointerException is a runtime discovery that references NULL objects when they cannot be referenced
Inputmischasexception is wrong type found when inputting
C. The most common error found in arithmeticexception calculation is the division by 0 operation
ArrayIndexOutOfBoundsException array is out of bounds. java does not check data out of bounds before compilation
D.ClassNotFoundException didn't find any related classes. The compiler checked them directly when writing the program
ClassCastException type conversion exception. It is common that an error occurs during the next transformation. The same as the compiler check
5.B
Parsing: a custom exception will be thrown when - 1 is entered to end the try catch run
2, Short answer questions
1.https://www.cnblogs.com/lcl-dcr/p/7653274.html
2.https://blog.csdn.net/qq_18505715/article/details/76696439
3.https://blog.csdn.net/uniquewonderq/article/details/46426383
4.throws is used to declare an exception, which is used in method declaration; throws is used to throw an exception, which is used inside the method body;
3, Programming questions
1.
import java.util.Scanner; public class Throws extends Exception{ void gradeException(){ System.out.printf("Score can only be 0-100 Between\n"); } } class ch6_1{ public static void main(String[] args) throws Throws{ Scanner input = new Scanner(System.in); float grade = 0.0f; while (true) { try{ System.out.println("Please enter the score:"); grade = input.nextFloat(); if(grade > 100 || grade <0) throw new Throws(); } catch (Throws e){ e.gradeException(); } } } }
2.
import java.util.Scanner; public class IllegalArgumentException extends Exception { void IllegalArgumentException(int a,int b, int c){ System.out.printf("\n%d,%d,%d Can't form a triangle",a,b,c); } } class isTriangle { void isTriangle(int a, int b, int c) throws IllegalArgumentException { int max = Math.max(Math.max(a, b), c); int min = Math.min(Math.min(a, b), c); int sum = a + b + c; int twoEdgeDec = sum - max - min - min; //Small difference between the two sides int twoEdgeAdd = sum - max; //The sum of the smaller sides //The sum of the two sides is less than the third side, and the difference between the two sides is greater than the third side if (max >= twoEdgeAdd || min <= twoEdgeDec) throw new IllegalArgumentException(); else System.out.printf("Can form a triangle"); } } class ch6_2{ public static void main(String[] args) { int a[] = new int[3]; Scanner input = new Scanner(System.in); System.out.print("Please enter three sides of triangle:"); for(int i = 0;i < 3; i++){ a[i] = input.nextInt(); } System.out.print("Three sides:"); for(int i : a){ System.out.printf("\t%d",i); } isTriangle isTriangle = new isTriangle(); try{ isTriangle.isTriangle(a[0],a[1],a[2]); } catch (IllegalArgumentException e){ e.IllegalArgumentException(a[0],a[1],a[2]); } } }
3.
import java.util.Scanner; public class aver { } class Throws extends Exception{ void gradeException(){ System.out.printf("Score must be positive or 0\n"); } } class ch6_1{ public static void main(String[] args) throws Throws{ Scanner input = new Scanner(System.in); float grade = 0.0f; float aver = 0.0f; float sum = 0.0f; int stuNum = 0; System.out.print("Please input the number of students:"); stuNum = input.nextInt(); for(int i = 0 ; i < stuNum ; i++ ){ try{ System.out.println("Please enter the student score:"); grade = input.nextFloat(); if(grade > 100 || grade <0) { i--; throw new Throws(); } else { sum = sum + grade; aver = sum/stuNum; System.out.printf("Total score:%3.2f \t Average:%3.2f \n",sum,aver); } } catch (Throws e){ e.gradeException(); } } } }