4

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??
        }
    }
}
3
  • 1
    you should post the error to, so we could help you better. Commented Aug 26, 2012 at 13:54
  • A follow-up question here: stackoverflow.com/questions/12130800/… Commented Aug 26, 2012 at 14:24
  • The follow up is slightly different and slightly related, I'm kinda happy to allow both. Commented Aug 28, 2012 at 2:59

5 Answers 5

6

If you see the 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)

which clearly states The local variable a may not have been initialized

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

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

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

float a = 0;
Sign up to request clarification or add additional context in comments.

2 Comments

after initialization Undefined variable or class name: ae System.out.println("Print hoga"+ae.getMessage());
I corrected that... try System.out.println("Print hoga"+e.getMessage()); but that too after you initialize a
3

"if i am trying to print value of "a" why its showing error?

Because dividing by zero throws an exception before a is initialized.

For printing the error you can print exception message or the whole stacktrace:

catch(ArithmeticException e)
{
   System.out.println(e.getMessage());
   e.printStackTrace();
}

Comments

3

a doesn't have any value. As exception happened in 44/d; statement as no value is in a probably.

Ankit1.java:14: variable a might not have been initialized
            System.out.println("Print hoga"+a);//why error come??

Its because the variable a is not initialized.

Also there won't be any ArithmeticException thrown for this 44/d statement because it has float operation so there is no Divide-by-zero Exception Instead Infinity will be the result.
For more see here

1 Comment

@Ankit yes. Printing a variable that is not being initialized causes an error.
2

a wasn't initialized
initialize default values for d and a

float d = 0.0f;  
float a = 0.0f;  

or use Float instead of float

Float a = null;  

1 Comment

Undefined variable or class name: ae System.out.println("Print hoga"+ae.getMessage());//it is showing this error
1

You define float d,a; but you are not initialize them. If you don't also later, before you use them it is a compile time error.
In your try you do:
d=0;
a=44/d;

But since you initialize them in a try and you access them inside the catch the compiler complains that a is not initialized. If you replaced with d you would also get the same error.
To solve this do:

float d = 0,a = 0;

Always initialize your local variables

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.