unexpected error

M

Madhur Ahuja

Consider this code segment:
class wrap
{
public static void main(String args[])
{
int n;
try
{
n=Integer.parseInt(args[0],10);
}
catch(Exception ArrayIndexOutOfBoundsException)
{
System.out.print("pass the argument");
System.exit(0);
}
System.out.println(n*2);
}
}

Compilation produces this error:
E:\programs\java1\wrap.java:15: variable n might not have been initialized
System.out.println(n*2);
^
I wonder why compiler produces this error, since the statement
n=Integer.parseInt(args[0],10); and produce the valid output since the
exception
is handled.

What should I do to avoid this kind of error.


--
Winners dont do different things, they do things differently.

Madhur Ahuja
India

Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com
 
K

kaeli

What should I do to avoid this kind of error.

Initialize n.

int n = 0;

See, you only catch one exception type. There are others as well as errors
that can't be caught. For example, you're parsing something without checking
that it's even a number. Or what if there is an IO error and it can't read
from stdin?

--
 
O

Oscar kind

Madhur Ahuja said:
Consider this code segment:
class wrap
{
public static void main(String args[])
{
int n;
try
{
n=Integer.parseInt(args[0],10);
}
catch(Exception ArrayIndexOutOfBoundsException)
{
System.out.print("pass the argument");
System.exit(0);
}
System.out.println(n*2);
}
}

Compilation produces this error:
E:\programs\java1\wrap.java:15: variable n might not have been initialized
System.out.println(n*2);
^
I wonder why compiler produces this error, since the statement
n=Integer.parseInt(args[0],10); and produce the valid output since the
exception
is handled.

How about NumberFormatException (thrown by Integer#parseInt(String, int))?
 
F

Filip Larsen

Madhur Ahuja wrote
Consider this code segment:
class wrap
{
public static void main(String args[])
{
int n;
try
{
n=Integer.parseInt(args[0],10);
}
catch(Exception ArrayIndexOutOfBoundsException)
{
System.out.print("pass the argument");
System.exit(0);
}
System.out.println(n*2);
}
}

Compilation produces this error:
E:\programs\java1\wrap.java:15: variable n might not have been initialized
System.out.println(n*2);
^
I wonder why compiler produces this error, since the statement
n=Integer.parseInt(args[0],10); and produce the valid output since the
exception is handled.

If you get an exception during the evaluation of the value you are going
to assign n, it will never be assigned which is what the compiler is
complaining about. So, either assign a value when you declare n (like
'int n = 0;'), assign a value in the catch, or move the usage of n (the
print statement) into the try-block after the assignment.



Regards,
 
J

Jacob

Oscar said:
Madhur Ahuja said:
Consider this code segment:
class wrap
{
public static void main(String args[])
{
int n;
try
{
n=Integer.parseInt(args[0],10);
}
catch(Exception ArrayIndexOutOfBoundsException)
{
System.out.print("pass the argument");
System.exit(0);
}
System.out.println(n*2);
}
}

Compilation produces this error:
E:\programs\java1\wrap.java:15: variable n might not have been initialized
System.out.println(n*2);
^
I wonder why compiler produces this error, since the statement
n=Integer.parseInt(args[0],10); and produce the valid output since the
exception
is handled.


How about NumberFormatException (thrown by Integer#parseInt(String, int))?

He actually catches this (and all others);
Its just a silly varaible name that confuses you :)
 
J

Jacob

kaeli said:
Initialize n.

int n = 0;

No. It will remove the compile error, but it leaves
the program with a flawed design: An unused (because
of the exit in the catch block) variable (with a phony
value) is alive outside of its natural scope.

Do it simple:

public static void main (String[] args)
{
try {
int n = Integer.parseInt (args[0],10);
System.out.println (n * 2);
}
catch (Exception exception) {
System.out.print ("pass an integer argument");
}
}
 
L

Liz

Jacob said:
Oscar said:
Madhur Ahuja said:
Consider this code segment:
class wrap
{
public static void main(String args[])
{
int n;
try
{
n=Integer.parseInt(args[0],10);
}
catch(Exception ArrayIndexOutOfBoundsException)
{
System.out.print("pass the argument");
System.exit(0);
}
System.out.println(n*2);
}
}

Compilation produces this error:
E:\programs\java1\wrap.java:15: variable n might not have been initialized
System.out.println(n*2);
^
I wonder why compiler produces this error, since the statement
n=Integer.parseInt(args[0],10); and produce the valid output since the
exception
is handled.


How about NumberFormatException (thrown by Integer#parseInt(String,
int))?

He actually catches this (and all others);
Its just a silly varaible name that confuses you :)

Are you not intrigued by a method in class Integer
that doesn't return an Integer?
 
H

Hemal Pandya

Madhur Ahuja said:
Consider this code segment:
class wrap
{
public static void main(String args[])
{
int n;
try
{
n=Integer.parseInt(args[0],10);
}
catch(Exception ArrayIndexOutOfBoundsException)
{
System.out.print("pass the argument");
System.exit(0);
}
System.out.println(n*2);
}
}

Compilation produces this error:
E:\programs\java1\wrap.java:15: variable n might not have been initialized
System.out.println(n*2);
^
I wonder why compiler produces this error, since the statement
n=Integer.parseInt(args[0],10); and produce the valid output since the
exception
is handled.

I believe the reason for this error is that the compiler does not
recognize that System.exit does not return. If indeed it did return
then n would be unitialized when control reached System.out statement.
This is demonstrated by the following simpler example which produces
the same error:

class Foo{
void bar(){
int n;
System.exit(0);
System.out.println(n);
}
}

I remember reading some discussion that it is quite impossible to
determine whether a method returns normally and therefore not
returning a value is not an error in a C++ function. Something about
global analysis.
What should I do to avoid this kind of error.

Personally, I like Jacob's reply most, to move the use for the
variable inside the try block. At other times I put a throw
RuntimeException statement just so the compiler recognizes that the
control does not fall through.
 
E

Eric Sosman

Liz said:
Are you not intrigued by a method in class Integer
that doesn't return an Integer?

It doesn't seem at all surprising or unusual to me.
The Object class, for example, has eleven methods of
which only three return Objects. The Math class has
thirty-two methods, *none* of which returns a Math.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top