201874040116- Li Xin "object-oriented programming (java)" Week 10 learning summary

 

project

content

This work belongs courses

 https://www.cnblogs.com/nwnu-daizh/

Where this requirement in the job

https://www.cnblogs.com/nwnu-daizh/p/11435127.html 

Job learning objectives

  

 

1. Master java exception handling techniques;

2. Understand the assertion of usage;

3. understand the purpose of the log;

4. Master program basic debugging techniques.

 

Part I: summary of Chapter VII of theoretical knowledge
  1. abnormal (exception):

    1) Abnormal object is an object instance of a derived class Throwable encapsulates the error.

    2) abnormal has its own grammar and specific inheritance structure.

    3) all exceptions are inherited from the Throwable, but the next layer into two branches: Error and Exception

      1.Error class hierarchy describes internal errors and resource depletion Java run-time system error.

      2.Exception hierarchy is divided into two branches: a program error caused by the abnormality belongs RuntimeException, for example: the type of error conversion; out of bounds array access; access null pointer. Another branch contains other exceptions.  

  2. throw (throws) Exception : in the methods of throw the exception statement to indicate throws clause.

    1) throws clause can simultaneously indicate a plurality of exception, this method will not be described these exception processing, but that throw thereof.

    2) a method must declare all the method may throw checked exceptions, without abnormalities or uncontrollable (Error), or should be avoided (RuntimeException).

    3) throws an exception object is achieved by the throw statement. throws new EOFException

   3. Create exception classes

    1) standard exception classes when not adequately describe the problem clearly.

    2) substantially identical to create a derived class, generally it should include a default constructor is described in detail and comprises a builder information.

  4. catch the exception

    1) has not been caught exceptions will result in the termination of the program.

    2) the need to use catch exceptions try / catch statement block

      1. If the try block code throws an exception, the program will skip the rest of the code in try block and a catch clause processor execute the code.

      2. How the try statement block does not throw an exception, then the program skips the catch clause.

  The stack trace:  program execution process of a method call list, which contains a specific position during program execution method invocation.

  6. Tips exception mechanism:

    1) instead of the exception processing is not simple test

    2) Do not excessively abnormal thinning

    3) using the exception hierarchy

    4) Do not suppress abnormal

    5) When an error is detected, "harsh" better than laissez-faire

    6) Do not be shy abnormal transfer

 

  7. assertion (assert):

    1) Syntax: assert condition or assert condition: expression

    2) erroneous assertion should only be used to determine the position and internal procedures in the testing phase.

Part II: Experimental part

  Experiment 1:

 Command line environment:

  Program 1:

 

  Program 2:

 

 Eclipse environment:

  Program 1:

 

  Program 2:

 

Untreated unchecked exceptions and checked exceptions difference is the error of the stage.

  Experiment 2:

  Test Procedure 1:

 

Code:

import java.util.*;

public class StackTraceTest
{
   /**
    * Computes the factorial of a number
    * @param n a non-negative integer
    * @return n! = 1 * 2 * . . . * n
    */
   public static int factorial(int n)
   {
      System.out.println("factorial(" + n + "):");
      var walker = StackWalker.getInstance(); //堆栈跟踪 
      walker.forEach(System.out::println);      
      int r;
      if (n <= 1) r = 1;
      else r = n * factorial(n - 1);
      System.out.println("return " + r);
      return r;
   }

   public static void main(String[] args)
   {
      try (var in = new Scanner(System.in))
      {
         System.out.print("Enter n: ");
         int n = in.nextInt();
         factorial(n);
      }
   }
}

result:

  Test Procedure 2 :

Code:

//积极的处理方式
import java.io.*;

public class ExceptionTest {
    public static void main (String args[])
   {
       try{
           
           FileInputStream fis=new FileInputStream("身份证号.txt");
           BufferedReader in =new BufferedReader(new InputStreamReader(fis));
           String m=new String();
           String n=new String();
           while((m=in.readLine())!=null) {
               n- + = m + "\ n-" ; 
           } 
           in.close (); 
           System.out.println (n-); 

       } 
       the catch (a FileNotFoundException E) { 
           System.out.println ( "file not found" ); 
           e.printStackTrace () ; 
       } the catch (IOException E) { 
           System.out.println ( "error student" ); 
           e.printStackTrace (); 
       } 
    
    } 
}
//消极的处理方式
import java.io.*;

public class a {
    public static void main (String args[]) throws  IOException
     {
         FileInputStream fis=new FileInputStream("身份证号.txt");
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
       String m, n = new String();
       while ((m = in.readLine()) != null) {
           n += m + "\n ";
       }
       in.close();
       System.out.println(n);
     }
}

result:

  

Experiment 3: Programming Exercises 

 

 

Experiment 4 : assert, logging, debugging skills validation experiments

  

  Experimental Procedure 1 :

 

After adding a comment:

 

 

  Experimental procedure 2, the experimental program 3  :

 

  

 

 

 

 

 

    

Guess you like

Origin www.cnblogs.com/whitepaint/p/11775796.html