This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes System.exit(0) in catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "System.exit(0) in catch block" Watch "System.exit(0) in catch block" New topic
Author

System.exit(0) in catch block

Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
I tried using the System.exit(0) to sucessfully avoid execute the finally block.
But is there any reason in a real time project why you would use such a statement in a catch block?


RB
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
There may not be any specific reason for not executing finally block,
the idea behind using System.exit(0) in a catch block might be just to come out of the program if any specific exception is thrown so that the further execution by any chance doesnt take place.

The statement just describes a possiblity when a finally block doesnt get to execute.
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
thanks for the post
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Think about a class that is used in many different systems. Suppose its methods discover serious corruption in the a data structure such that any further references to that structure, even just writing buffered data to disk in a finally clause, may result in further loss of data. The methods can unconditionally prevent any further processing by calling System.exit() in the catch block for the exception the signaled the error.

The problem with throwing exceptions is that too many programmers just catch Exception, perhaps print a message, and continue processing. This can be very dangerous if the exception indicated a serious problem rather just a NumberFormatException. System.exit() makes sure that no more damage is done before a human has a chance to look at the situation.
[ March 11, 2005: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
The System.exit(int) method is abused more often than not (at least in my experience). There are few justifiable reasons to abruptly terminate the VM and the consequences of this abuse are often misunderstood and neglected until the software makes it into production.

Care to share the real problem, rather than a possibly broken solution?


Tony Morris
Java Q&A (FAQ, Trivia)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Tony Morris said:
Care to share the real problem, rather than a possibly broken solution?

Tony, I don't understand your question.
[ March 11, 2005: Message edited by: Mike Gershman ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
I can't tell if the original question is a hypothetical, in which case, the answer is simply, "No, you don't call System.exit just because you're within a finally block - you must have a good justifiable reason to forcefully and abruptfully bring down the JVM."

If the original question is related to a specific problem, and "calling System.exit is the solution", then I have asked for the real problem, and not the possibly broken (is there a justified reason? I doubt it) solution.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: System.exit(0) in catch block
 
Similar Threads
execution of finally
Output
try catch finally
Return statement in try block
Unreachable statement