Exception classes and assertions

A. Three of programming errors

1. Syntax error: the lowest level of error found by the compiler.

2. Logical errors: the program does not perform as expected in the program. Bugs also known as logical errors (bugs) commissioning (the debugging) processing method which process is to find and correct errors are generally referred to as narrow method.

3. Run Error: If the environment can not be found a mistake in the execution occurs. Its approach is abnormal and assertions.

II. Exception class and assertions

1.try-catch statement
java using a try-catch statement to handle run-time error, is about to abnormal operating statements may appear in a try section, once part try to throw an exception (ie runtime error occurs in a certain try in the statement), then the section will end immediately try steering execution of a catch part, try-catch statement catch may consist of several components, namely treatment of the corresponding transmission abnormality, for example:

    try{
    包含的可能发送异常的语句
      }
    catch(ExceptionSubClass1 e){
    ....
    }
    catch(ExceptionSubClass2 e){
    ....
    }
    

For example:
If you are a middle school, you want to secretly go to the Internet cafe, but you are afraid to get caught, but you are very clever, you think of several ways to cope Method One: When you were arrested teacher when that happens, you will obediently admit to the teacher, and begged him not to tell your parents. Method two: parents when you are caught, you will be turned around and ran home, frantically doing housework. In this example, you go to the Internet this process could not go wrong, all the rest, and it will be errors, processing errors that may occur to you, so you come up with several ways. try-catch is used to hold these:

    try{
         你去上网;
    }
    catch(被老师抓住){
    乖乖认错;
    }
    catch(被父母抓住){
       跑回家做家务;
    }

2, affirm

assert booleanExpression:messageException;

In the form of assertion statements, when booleanExpression value (predicate) is true, program execution continues at the statement asserted, otherwise execution is stopped, and the output value messageException (string) expression. How to prompt the user for the emergence of error.

Above learning summary includes personal, such as the existence unreasonable, welcome to point out, learn from each other.

Published 35 original articles · won praise 0 · Views 1298

Guess you like

Origin blog.csdn.net/c1776167012/article/details/103795358