actually logically your program is wrong because the class Error and its subclass in java is not mean to catch them they represent the error in your code
and it is poor programming practice to catch the Runtime Exceptions and Error's
you should only catch the check Exception i.e. include IOException
Joined: May 11, 2004
Yes, Amit you are right that its not a good programming practice. But if you study Assertions, you will find that we can catch AssertionError if thrown in the code. So, if Sun Exam developer thinks that the examinee should know this fact then they are going to test your knowledge.
poor programming practice to catch the Runtime Exceptions and Error's
Agreed but there are exceptions to this rule. The problem is NumberFormatException is a RuntimeException, and this exception is often thrown when parsing user input (Integer.parseInt(args) for example). So a RuntimeException is not ALWAYS caused by a programming error as such. If you do not catch this exception, and display a meaningful error message, the clueless user is going to be confused by a possibly complex stack trace.
I decided it was time for me to register just to add a reply to this thread! I was wondering where you came across this piece of code. If you are studying for your SCJP exam then you should really be aware that this code flaunts the 'good practice' guidelines of assertions - that is they should not produce a side effect. In this case your assertion will decrement the i variable if the condition of the assertion evaluates to false.
Hope this helps too!
Tom Screen<br />SCJP 1.4
Joined: Aug 03, 2002
I was wondering where you came across this piece of code
Unfortunately, VINCE CARTER (and his namesake PETER CARTER) has not responded to our requests to quote the source of his code when he posts
I'm pondering on the option to close his questions if he continues.
Originally posted by amitkumar kamboj: it is poor programming practice to catch the Runtime Exceptions
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Sep 26, 2004
Even number : 0 Odd number : 1 Even number : 2 Odd number : 3 Even number : 4 Odd number : 5 Even number : 6 Odd number : 7 Even number : 8 Odd number : 9