Java是否自动处理异常,而不使用try和catchblock? [英] Does Java handle exception automatically without using a try and catchblock?

查看:145
本文介绍了Java是否自动处理异常,而不使用try和catchblock?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码显示异常:

Exception in thread "main" java.lang.ArithmeticException: / by zero
        at Ankit2.main(Ankit2.java:6)

为什么和如何发生?没有使用try和catch块?

Why and how its happening? Without using try and catch block?

class ankit1    
{
public static void main(String args[])    
 {
        float a=20,b=120,c=50,sum;    
        sum=(a+b+c)/0;    
        System.out.println("The average of three number is:"+sum);    
    }      

 }  


推荐答案

这是一个RuntimeException。

That is a RuntimeException.

您不必声明或捕获这些。

You do not have to declare or catch those.

任何异常你的代码没有捕获(声明或不声明),将使正在运行的线程崩溃。在启动程序的主线程的情况下,JVM将在退出之前打印堆栈跟踪。

Any exception that your code does not catch (declared or not), will crash the running thread. In the case of the main thread that started the program, the JVM will print the stacktrace before exiting.

这篇关于Java是否自动处理异常,而不使用try和catchblock?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆