Exploring the problem that the code continues to run after java throws an exception
1、
package com.baofoo.admin.test; import lombok.extern.slf4j.Slf4j; import org.junit.Test; /** * Created by BF100 on 2018/4/12. */ @ Slf4j public class TestByCaoxNew { @Test public void test1(){ try{ int a = 1/0; System.out.println("Did I execute it?"); }catch (Exception e){ log.error("call Exception :{}",e); e.printStackTrace (); } System.out.println("come on !!!"); } @Test public void test2(){ try{ if(true) { throw new Exception("I'm going to throw an exception!!!"); } // throw exception, will not execute System.out.println("Did I execute it?"); }catch (Exception e){ log.error("call Exception :{}",e); } System.out.println("I did it!!!"); } }2. Test1 running results:
3. Test2 running results: