• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

How to handle errors in java ?

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Actually we cannot predict when errors occurs in our programs like virtualmachineerror,threaddeath etc.,

Is there any chance to handle these type of errors ?
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can catch them but it would not be the right thing to do. Basically errors are something which crops up due to an error in the underlying enviornment. Though even if you catch it, I guess it would be quite difficult to correct it. So I would suggest it's better left unhandled.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats absolutely correct.

You got to remember one point:

Errors are DIFFERENT from Exceptions!.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As mentioned, Errors generally indicate an irrecoverable problem. Often, that means you should not catch them at all.

However, in a multi-threaded program, an Error in a background thread might result in its silent death and your great confusion. In such programs, you might want to install some sort of logging of all Throwables, when they are about to terminate a thread. Make sure that your logging code is very simple, so it can't have problems itself. Generally, rethrow the Throwable after logging it.

In some of our applications, we catch OutOfMemoryError. It is a bit different to other Errors. One cannot really recover fully from it, as one doesn't know how what state it was in when heap was exhausted, but one can attempt an orderly, logged shut-down.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OutOfMemory seems to be one you can handle. If you try to create a 12 meg buffer and get out of memory, try a 4 meg buffer? I've seen them in my HTTP server logs ... one request fails to allocate some huge object but the other threads go on happily enough.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Warning! Danger!

OutOfMemoryError can occur at any time, including half-way through a set of steps that normally either fully complete or fully roll back. The state of your Java program after OutOfMemoryError is often undefined and it is often unsafe to do anything beyond attempting an orderly shut-down of your program.

An exception to this would be if you enclose just one huge array allocation in a try..catch. In that situation, I think you can be sure that an OutOfMemoryError has not caused unexpected havoc. And, as you say, this is also a situation where you may genuinely be able to take alternative action, such as reducing the size of array you ask for.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Peter, Certainly what you told makes sense,
Is there any way to prevent this OutOfMemoryError,
One way to prevent this error,(by specifying the max and min heap size)
java -ms64m -mx256m CardsBatchProcess

Is there any other alternative ? or a better way ?

Thanks Ragav.
reply
    Bookmark Topic Watch Topic
  • New Topic