为什么未打印此异常?为什么显示错误? [英] Why is this exception is not printed? Why is it showing an error?

查看:307
本文介绍了为什么未打印此异常?为什么显示错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试打印 a的值,为什么显示错误?为什么异常会变成错误?

If I am trying to print the value of "a" why is it showing an error? Why has the exception become an error?

class Ankit1
{
    public static void main(String args[])
    {
        float d,a;
        try
        {
            d=0;
            a=44/d;
            System.out.print("It's not gonna print: "+a); // if exception doesn't occur then it will print and it will go on to the catch block
        }
        catch (ArithmeticException e)
        {
            System.out.println("a:" + a); // why is this an error??
        }
    }
}


推荐答案

如果看到错误

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The local variable a may not have been initialized

    at your.package.Ankit1.main(Ankit1.java:18)

其中明确指出局部变量a可能尚未初始化

由于未初始化变量 a ,因此出现此错误。

You're getting this error as your variable a wasn't initialized.

如果要打印错误消息,请尝试打印... e.getMessage() p.printStackTrace()堆栈跟踪。

And if you want to print the error message try printing... e.getMessage() or p.printStackTrace() for complete stack trace.

要修复此简单的初始化 a ,并使用类似这样的值...

To fix this simple initialize a with some value like this...

float a = 0;

这篇关于为什么未打印此异常?为什么显示错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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