This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes About  Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About  Error" Watch "About  Error" New topic

About Error

YingShiuan Chang

Joined: Feb 18, 2004
Posts: 6
An Error indicates serious problems that a reasonable application should not try to catch. When an error is produced, does the program break off or continue running? Or it depends on what kinds of errors?
For instance, when OutOfMemoryError is produced, what will it happen?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
An Error gets thrown like an Exception and can be caught like one. If the Error is not caught, the code that called your main method gets control and causes a stack trace to be printed. This will show you the type of Error, where it happened, and how you got there from your main method.
You can also catch an Error and try to handle it. For instance, you might designate some objects for garbage collection by setting their references to null. However, you should then do as little as possible (write critical data, release locked database records) and rethrow the error. This will give you the stack trace and allow the operator to cleanly restart the application. Remember that your execution environment may be corrupted as a result of the Error in a way you can't repair.

Mike Gershman
SCJP 1.4, SCWCD in process
YingShiuan Chang

Joined: Feb 18, 2004
Posts: 6
Thank you for your help.
I agree. Here's the link:
subject: About Error
It's not a secret anymore!