which exception?

Discussion in 'Java' started by josh, Feb 14, 2007.

  1. josh

    josh Guest

    Hi,

    which is the right exception to manage an "out of memory situation"
    when I create an object with the new operator?

    try
    {
    Time t = new Time();
    }catch(...)
    {
    //////// out of memory
    }

    Thanks
     
    josh, Feb 14, 2007
    #1
    1. Advertising

  2. josh schrieb:
    > Hi,
    >
    > which is the right exception to manage an "out of memory situation"
    > when I create an object with the new operator?


    If the system runs out of memory, an OutOfMemoryError is thrown. An
    Error (java.lang.Error) is not an Exception and therefore should not be
    caught.

    Bye
    Michael
     
    Michael Rauscher, Feb 14, 2007
    #2
    1. Advertising

  3. josh

    josh Guest

    On 14 Feb, 10:29, Michael Rauscher <> wrote:
    > josh schrieb:
    >
    > > Hi,

    >
    > > which is the right exception to manage an "out of memory situation"
    > > when I create an object with the new operator?

    >
    > If the system runs out of memory, an OutOfMemoryError is thrown. An
    > Error (java.lang.Error) is not an Exception and therefore should not be
    > caught.
    >
    > Bye
    > Michael


    My problem is to convert C++ code like this:

    Time *t;

    try
    {
    t = new Time(15,10,00);
    }
    catch(bad_alloc ba)
    {
    cout << "OUT OF MEMORY!!\n";
    return EXIT_FAILURE;
    }

    so how I can do that?
     
    josh, Feb 14, 2007
    #3
  4. On Feb 14, 8:13 pm, "josh" <> wrote:
    ....
    > which is the right exception to manage an "out of memory situation"
    > when I create an object with the new operator?


    'That does not happen.'

    Or rather, it would be exceptional for
    it to happen on the creation of 'a single
    object' as suggested in the code snippet.

    It might happen after creating 100's of
    thousands, or millions of average objects,
    but the JVM can work with astonishing
    amounts of data before hitting memory
    problems, even with the default memory
    size.

    Much more common in this situation, is
    that the application has a memory leak.

    I sughgest you prepare an SSCCE*, and
    if that fails to reveal the problem,
    post it here.

    * <http://www.physci.org/codes/sscce>

    In addition to what Michael was mentioning,
    I will add that although Error's should
    generally not be caught, an OOME *can*
    be caught, and with a little preparation,
    it is possible to show the end user an
    'Error' dialog (at least) before the
    program shuts down.

    But look into the SSCCE first.

    Andrew T.
     
    Andrew Thompson, Feb 14, 2007
    #4
  5. On 14 Feb 2007 01:13:30 -0800, josh wrote:
    > which is the right exception to manage an "out of memory situation"
    > when I create an object with the new operator?


    If the constructor fails, an appropriate exception will be raised.

    Why do you want to replace it with a different one, especially if you
    don't know which what you want to replace it with?

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Feb 14, 2007
    #5
  6. josh wrote:
    > My problem is to convert C++ code like this:
    >
    > Time *t;
    >
    > try
    > {
    > t = new Time(15,10,00);
    > }
    > catch(bad_alloc ba)
    > {
    > cout << "OUT OF MEMORY!!\n";
    > return EXIT_FAILURE;
    > }
    >
    > so how I can do that?
    >


    It's easy: don't mind the "out of memory situation".

    The Java equivalent would be:

    Time t = new Time(15,10,00);

    There is a distinction in Java between Errors and Exceptions (both are
    subtypes of java.lang.Throwable). Errors indicates conditions where it
    doesn't make (much) sense to handle them - more: the documentation says
    that one should not handle them.

    Bye
    Michael
     
    Michael Rauscher, Feb 14, 2007
    #6
  7. josh

    Chris Dollin Guest

    josh wrote:

    > My problem is to convert C++ code like this:
    >
    > Time *t;
    >
    > try
    > {
    > t = new Time(15,10,00);
    > }
    > catch(bad_alloc ba)
    > {
    > cout << "OUT OF MEMORY!!\n";
    > return EXIT_FAILURE;
    > }
    >
    > so how I can do that?


    Time t = new Time( 15, 10, 00 );

    If it runs out of memory, an error is thrown and the program will
    terminate, just as the C++ does.

    (Unless there's an outer shell that catches and handles it, of
    course, which in principle there /could/ be. But then, you shouldn't
    try and second-guess that.)

    --
    Chris "electric hedgehog" Dollin
    "It's just the beginning we've seen" - Colosseum, /Tomorrow's Blues/
     
    Chris Dollin, Feb 14, 2007
    #7
  8. josh

    josh Guest


    > If the constructor fails, an appropriate exception will be raised.


    what? If I didn't want a generic Exception
     
    josh, Feb 14, 2007
    #8
  9. josh

    Chris Dollin Guest

    Chris Dollin wrote:

    >> try
    >> {
    >> t = new Time(15,10,00);
    >> }
    >> catch(bad_alloc ba)
    >> {
    >> cout << "OUT OF MEMORY!!\n";
    >> return EXIT_FAILURE;
    >> }
    >>
    >> so how I can do that?

    >
    > Time t = new Time( 15, 10, 00 );
    >
    > If it runs out of memory, an error is thrown and the program will
    > terminate, just as the C++ does.


    Duh. Somehow I read an `exit(EXIT_FAILURE)` rather than a `return`
    up there. Today, I'm an idiot. Ignore my previous post.

    --
    Chris "electric idiot" Dollin
    There' no hortage of vowel on Uenet.
     
    Chris Dollin, Feb 14, 2007
    #9
  10. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    josh schreef:
    >> If the constructor fails, an appropriate exception will be raised.

    >
    > what? If I didn't want a generic Exception


    It is not a generic Exception. It is an ‘appropriate exception’. In
    this case, even an Error: OutOfMemoryError

    H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)

    iD8DBQFF0ymNe+7xMGD3itQRAnvgAJ0cN0LqtolbFVUBSfS+07wfd9Na5wCeJQ+b
    qrQrvg9lxhCfrwpK4hi47Bw=
    =U4rS
    -----END PGP SIGNATURE-----
     
    Hendrik Maryns, Feb 14, 2007
    #10
  11. josh

    Chris Uppal Guest

    josh wrote:

    > My problem is to convert C++ code like this:
    >
    > Time *t;
    >
    > try
    > {
    > t = new Time(15,10,00);
    > }
    > catch(bad_alloc ba)
    > {
    > cout << "OUT OF MEMORY!!\n";
    > return EXIT_FAILURE;
    > }


    The API implied by the C++ code fragment is not suitable for use in Java.
    Normally it is a bad idea to return error status for exeptional conditions,
    exceptions are intended to be used in such circumstances. So, a first
    approximation to that code (in Java) would be

    try
    {
    t = new Time(15,10,00);
    }
    catch (/* what goes here ??*/)
    {
    system.err.println("Out of Memory !!");
    throw new MyOutOfMemoryException();
    }

    But that has some problems -- it's not likely that you'd be able to write to
    system.err if the program was out of memory, for one (though there /might/ be
    more sophisticated ways of dealing with the problem which /did/ allow the
    program to carry on). So, we might have

    try
    {
    t = new Time(15,10,00);
    }
    catch (/* what goes here ??*/)
    {
    throw new MyOutOfMemoryException();
    }

    But that, for most purposes is redundant, since the exeption that the system
    throwns (OutOfMemoryError) is sufficient on its own. So we can remove both the
    catch and the throw:

    t = new Time(15,10,00);

    Or -- if you've managed to find a way to allow your program to continue after
    an OOME (not easy) -- then you might have

    try
    {
    t = new Time(15,10,00);
    }
    catch (OutOfMemoryError e)
    {
    invokeCleverRecoveryCode();
    throw new MyOperationFailedException(e);
    }

    But that would be pretty unusual....

    -- chris
     
    Chris Uppal, Feb 14, 2007
    #11
  12. josh

    Daniel Pitts Guest

    On Feb 14, 2:36 am, Chris Dollin <> wrote:
    > Chris Dollin wrote:
    > >> try
    > >> {
    > >> t = new Time(15,10,00);
    > >> }
    > >> catch(bad_alloc ba)
    > >> {
    > >> cout << "OUT OF MEMORY!!\n";
    > >> return EXIT_FAILURE;
    > >> }

    >
    > >> so how I can do that?

    >
    > > Time t = new Time( 15, 10, 00 );

    >
    > > If it runs out of memory, an error is thrown and the program will
    > > terminate, just as the C++ does.

    >
    > Duh. Somehow I read an `exit(EXIT_FAILURE)` rather than a `return`
    > up there. Today, I'm an idiot. Ignore my previous post.
    >
    > --
    > Chris "electric idiot" Dollin
    > There' no hortage of vowel on Uenet.



    In C++, I thought the "new" operator returned NULL if there wasn't
    enough memory...

    In any case, The concerns of Memory management are different in Java
    than in C++. Don't try to "handle" out of memory exceptions, as they
    generally imply a bug, rather than a true resource limitation.
     
    Daniel Pitts, Feb 14, 2007
    #12
  13. "josh" <> writes:

    > try
    > {
    > t = new Time(15,10,00);
    > }
    > catch(bad_alloc ba)
    > {
    > cout << "OUT OF MEMORY!!\n";
    > return EXIT_FAILURE;
    > }


    Not necessarily "out of memory", but "out of free chunks of contigous
    memory the size of the object". Java manages the memory - there is a
    reason people write virtual memory/garbage collection systems for C++
    programmers. So just don't worry about it.
     
    Tor Iver Wilhelmsen, Feb 15, 2007
    #13
    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. John
    Replies:
    0
    Views:
    361
  2. Familyman
    Replies:
    3
    Views:
    525
    Alan J. Flavell
    Feb 9, 2006
  3. Kenny
    Replies:
    0
    Views:
    467
    Kenny
    Jun 2, 2004
  4. chetan
    Replies:
    5
    Views:
    376
    John Harrison
    Sep 21, 2004
  5. DanielEKFA
    Replies:
    8
    Views:
    615
    DanielEKFA
    May 16, 2005
Loading...

Share This Page