e.printStackTrace()会不会抛出异常,终止程序

  • 在开发中我们通常会用try/catch来捕获遇到的异常,但是通过看源代码以及测试我们可以发现e.printStackTrace()这个方法是无返回值的,在当Java程序抛出异常时,它会在调用堆栈中创建一个异常对象并将其抛出。使用e.printStackTrace()方法可以打印出这个异常对象所在的调用堆栈,包括哪个类的哪个方法抛出了异常,以及异常被传播的路径。所以,e.printStackTrace()只会打印出异常,而不会抛出异常以及中断程序。
		try {
		//执行代码
        } catch (Exception e) {
            e.printStackTrace();
        }
  • 如果我们想抛出异常可以像下面这样:
		try {
		//执行代码
        } catch (Exception e) {
            throw new Exception();
        }

测试:

	@Test
    public void exceptionTest(){
            try {
                System.out.println(0/0);
            }catch(Exception e){
                e.printStackTrace(); // printStackTrace只是打印错误日志,并不中断程序
                System.out.println("printStackTrace 之后的代码可以执行");
            }
        System.out.println("判断能不能执行:可以");
            
    }

执行结果:

"C:\Program Files\Java\jdk1.8.0_20\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:Q:\WorkSoftware\IDEA\IntelliJ IDEA 2021.2.1\lib\idea_rt.jar=58849:Q:\WorkSoftware\IDEA\IntelliJ IDEA 2021.2.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\PC\AppData\Local\Temp\classpath1595811700.jar com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 CommonTest,exceptionTest
java.lang.ArithmeticException: / by zero
	at CommonTest.exceptionTest(CommonTest.java:45)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:483)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
printStackTrace 之后的代码可以执行
判断能不能执行:可以

Process finished with exit code 0

你可能感兴趣的:(随笔--oo--,java)