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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这段代码是一个无限循环,每次循环先等待20分钟,然后弹出一个提示框,提示用户休息20秒,并等待20秒后关闭提示框,然后进入下一次循环。 具体分析如下: 1. while (true) { // 无限循环 这是一个无限循环,表示程序一直执行下去,直到被手动终止。 2. try { Thread.sleep(20 * 60 * 1000); // 等待20分钟 } catch (InterruptedException e) { e.printStackTrace(); } 这里使用了 Thread.sleep() 方法来让程序等待20分钟,单位是毫秒,所以要乘以60和1000。如果在等待过程中被中断抛出 InterruptedException 异常,需要进行处理。 3. JOptionPane.showMessageDialog(null, "休息20s"); // 弹出提示框 这里使用了 JOptionPane 类中的 showMessageDialog() 方法来弹出一个提示框,提示用户休息20秒。第一个参数为弹出框的父窗口,因为这里使用的是 null,所以弹出框居中显示在屏幕上。 4. try { Thread.sleep(20 * 1000); // 等待20秒 } catch (InterruptedException e) { e.printStackTrace(); } 这里再次使用 Thread.sleep() 方法来让程序等待20秒,单位同样是毫秒。如果在等待过程中被中断抛出 InterruptedException 异常,需要进行处理。 5. JOptionPane.getRootFrame().dispose(); // 关闭提示框 这里使用了 JOptionPane 类中的 getRootFrame() 方法来获取提示框的顶层窗口,然后调用 dispose() 方法来关闭提示框。 6. 循环回到第一步,继续等待并弹出提示框。 总的来说,这段代码的作用是让程序每隔20分钟提醒用户休息20秒,以保护用户的身体健康。但是这样的代码应该放在单独的线程中执行,以避免阻塞主线程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值