java training: Exception (try-catch execution order and custom exception)

Keywords:
try: code execution may produce abnormal
catch: catch the exception
finally: whether abnormal code is always executed
throws: Statement methods may be thrown by various exceptions
throw: Manually throw custom exception
With exception handling try-catch-finally:
I. (normal operation, without exception):
After execution try, the finally, the finally block statement
Case 2 (abnormal try in)
When performing abnormality statements (statement after the try statement is not executed in the abnormality), skip to catch block, and then finally performed, and then execute the statement after the finally
public  class Test2 {
     public  static  void main (String [] args) { 
        Scanner in = new new Scanner (the System. in ); 
         the System. OUT .println ( " Please enter the dividend: " );
         the try { // the possible abnormal start code into the try 
            int num1 = in .nextInt (); 
            . the System OUT .println ( " acceptance divisor " );
             int num2 = in .nextInt (); 
            the System. OUT.println (num1 + " / " + num2 + " = " + num1 / num2);
             // return;
             // exception block: catch and finally at least one must be present, try block must 
        } the catch (a InputMismatchException E) { // the try in abnormal matches, the statement after the execution anomalies are not directly invoke catch, and then execute the program after the catch block, if unusual but does not match, the catch block is not executed, the routine ends directly 
            System. OUT .println ( " error, dividend and divisor must be an integer " );
             // e.printStackTrace (); // output exception stack information, print error sequence, the calling procedure 
            e.getMessage ();
             // System.exit (1); 
            return ;
        } Catch (an ArithmeticException E) { 
            System. OUT .println ( " dividend is not 0 " ); 
            e.printStackTrace (); 
        } the finally { // whether an abnormality occurs, the abnormality match, the finally will be executed, if the catch call System. exit (1); and finally, after finally clauses do not perform 
            the System. OUT .println ( " Thank you for using " ); 
        } 
        the System. OUT .println ( " after finally statement " );
try special circumstances contained in return:
. A no abnormality (execution order):
Codes before 1.return 4. 2.finally 3.return statement execution is completed (it does not execute a return statement, the statement is not executed after the finally block)
. B abnormal (execution order):
1.异常之前与异常代码 2.catch语句 3.finally块 4.finally块之后语句 5.执行完毕(因为异常直接跳到catch块,所以try中return不会执行)
 
特殊情况之catch中含有return
a.无异常(执行顺序):
1.执行try代码块 2.执行finally块 3.执行finally之后的语句 4.退出(无异常,所以不执行catch,直接执行finally)
b.有异常(执行顺序):
这里有两种情况
情况一(异常匹配到的代码块中有return):
1.执行try块(异常语句后的try语句均不执行)2.执行catch块return之前的语句 3.执行finally块 4.执行catch中的return语句 5.退出
情况二(异常匹配到的代码块无有return,catch在其他chtch块中)
执行到异常语句时(不执行try中异常语句之后的语句),直接跳到catch块,然后执行finally,再执行finally之后的语句
catch块的执行原理:
对于异常的捕获,可以有多个catch,对于try发生的异常,他会根据发生的异常和catch里面的异常类型进行匹配(按照catch块从上往下匹配,但并不会执行不匹配的catch块语句),当他匹配到某个catch块时,他就直接进入这个catch块内,忽略这个catch块后面的所有catch块,所以,一般将catch(Exception e)放在最后,catch块承先子类后父类排列。
 
注意:无论是否发生异常,异常是否匹配,均会执行finally块,阻止finally执行的唯一方法是在catch块中调用 System.exit(1) 语句(System.exit(1)表示非正常退出程序)。
 
 
用 throw 进行异常处理:
开发方法遇到异常时,开发人员不一定要一定去处理这个异常,可以用throws向上继续抛出这个异常(异常可以抛出多个,每个用逗号隔开),强制调用者处理这个异常,调用者可以用try-catch处理,也可以继续向上声明,若在main方法处继续向上声明,就会让jvm去处理这个异常。
自定义异常:
第一步:若需要自定义异常名字,需新建一个类MyException,并让它继承Exception;重写构造方法,
若不需要自定义异常名字,则可以略过第一步,在第二步直接书写 throw new Exception("");
public class MyEcxeption extends Exception {
    public MyEcxeption(String message) {
        super(message);
    }
}
第二步:在你需要的类中引用他(需要用关键字throw),注意,在引用类必须抛出这个异常
public void setSex(String sex) throws MyException{
    if (sex.equals("") || sex.equals("") ){
        this.sex = sex;
    }else {
        throw new MyException("性别必须为男或女");
    }

}
throw与throws的区别:
throw自行定义并抛出异常,位于方法体内部,只可以跟一个异常对象
throws抛出异常,位于方法参数列表后,可以跟多个异常类
 

Guess you like

Origin www.cnblogs.com/chengxuxiaoman/p/10960822.html