System.exit(1) never returns

R

Roedy Green

Consider this code:

try
{
in = fileOpen();
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(1);
}
If you try to use variable in, Java warns it may not be initialised.
It thinks System.exit() may return.

I have put code in like this:

catch ( IOException e )
{
e.printStackTrace();
System.exit(1);
return;
}

to reassure it, though that looks goofy. What do you do?
 
H

Harald Hein

Roedy Green said:
to reassure it, though that looks goofy. What do you do?

Do what the compiler asks for, initialize the variable:

in = null; // that's all you need
try {
in = fileOpen();
} catch ( IOException e ) {
e.printStackTrace();
System.exit(1);
}
 
J

Joseph Millar

Consider this code:

try
{
in = fileOpen();
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(1);
}
If you try to use variable in, Java warns it may not be initialised. [snip!]
to reassure it, though that looks goofy. What do you do?

Well, I avoid System.exit() when at all possible ;-). Baring that,
I make sure "in" has an initial value, usually when it is declared,
like:

FileInputStream in = null;

This shuts the compiler up as there is an explicit assigned initial
value.

--Joe
 
F

Frank

Consider this code:

try { in = fileOpen();
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(1);
}
If you try to use variable in, Java warns it may not be initialised.
It thinks System.exit() may return.

I have put code in like this:

catch ( IOException e )
{
e.printStackTrace();
System.exit(1);
return;
}

to reassure it, though that looks goofy. What do you do?

I generally try to avoid System.exit when at all possible, in this case,
favoring something more like:

catch (IOException e) {
throw new RuntimeExceptionSubclass(e);
}

HTH
Frank
 
D

Dale King

Consider this code:

try
{
in = fileOpen();
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(1);
}
If you try to use variable in, Java warns it may not be initialised.
It thinks System.exit() may return.

I have put code in like this:

catch ( IOException e )
{
e.printStackTrace();
System.exit(1);
return;
}

to reassure it, though that looks goofy. What do you do?

I would suggest instead throwing an exception after it. If it does return
that would be an error condition. Java doesn't have a way to mark that a
method never returns.
 
D

Dario

Roedy said:
If you try to use variable in, Java warns it may not be initialised.
It thinks System.exit() may return.

It's true.
Sometime System.exit may throw an exception.
The specification says:

Throws:
SecurityException - if a security manager exists and its
checkExit method doesn't allow exit with the specified status.

- Dario
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top