System.exit(1) never returns

Discussion in 'Java' started by Roedy Green, Aug 17, 2003.

  1. Roedy Green

    Roedy Green Guest

    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?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 17, 2003
    #1
    1. Advertising

  2. Roedy Green

    Harald Hein Guest

    "Roedy Green" wrote:

    > 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);
    }
     
    Harald Hein, Aug 17, 2003
    #2
    1. Advertising

  3. On Sun, 17 Aug 2003 02:08:21 GMT, Roedy Green <> wrote:

    > 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
     
    Joseph Millar, Aug 17, 2003
    #3
  4. Roedy Green

    Frank Guest

    On Sun, 17 Aug 2003 02:08:21 GMT, Roedy Green <> wrote:

    > 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

    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
     
    Frank, Aug 18, 2003
    #4
  5. Roedy Green

    Dale King Guest

    In article <>,
    says...
    > 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.
    --
    Dale King
     
    Dale King, Aug 22, 2003
    #5
  6. Roedy Green

    Dario Guest

    Roedy Green wrote:

    > 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
     
    Dario, Aug 22, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Mitchell
    Replies:
    8
    Views:
    1,763
    Steven Cheng[MSFT]
    Feb 22, 2005
  2. Soren Kuula
    Replies:
    1
    Views:
    474
    Henry S. Thompson
    Dec 1, 2005
  3. Kevin
    Replies:
    4
    Views:
    447
    Irrwahn Grausewitz
    Oct 17, 2003
  4. Zhidian Du

    system() never returns, how to kill it?

    Zhidian Du, Nov 25, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    104
    Sisyphus
    Nov 26, 2003
  5. kaleem
    Replies:
    8
    Views:
    862
    Tad McClellan
    Dec 14, 2006
Loading...

Share This Page