Results 1 to 7 of 7
Thread: when to use exceptions
- 07-21-2013, 08:52 PM #1
Member
- Join Date
- Jul 2013
- Posts
- 52
- Rep Power
- 0
when to use exceptions
I have read through about exceptions and it is mentioned that exception handling usually requires more time and resources and it is advisable that do not use exceptions whenever it is possible to deal with simple, expected situations.
So, does that means that division by zero should not be handle by Arithmetic Exception since it can be done in following way.
If so, then when should ArithmetricException be used?
Java Code:int number1; int number2; if (number2 == 0) System.out.println("Divisor cannot be zero") else System.out.println(number1/number2);
Similarly, null pointer can be done in following way. Am I wrong?
Java Code:if(pointera!= null) System.out.println(pointera.toString()); else System.out.println("pointera is null");
Last edited by dojob; 07-21-2013 at 08:55 PM.
- 07-21-2013, 11:44 PM #2
Re: when to use exceptions
You didn't bother to reply to two useful responses on your earlier thread. Why should we expect any better behavior this time round?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 07-22-2013, 05:56 AM #3
Member
- Join Date
- Jul 2013
- Posts
- 52
- Rep Power
- 0
- 07-22-2013, 07:44 AM #4
Re: when to use exceptions
Don't worry about micro-optimizations. If a method can't comply with its contract, it's almost always better to throw an exception than to count on the programmer checking the return value.
Get in the habit of using standard Java naming conventions!
- 07-22-2013, 05:31 PM #5
Member
- Join Date
- Jul 2013
- Posts
- 52
- Rep Power
- 0
- 07-22-2013, 05:43 PM #6
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: when to use exceptions
Yes!
and focus in clean and simple code?
Yes!
Generally, clean and simple code is actually more trivial, or as it is also known: the code is more "dumb".
The Developer Insight Series, Part 1: Write Dumb Code -- Advice From Four Leading Java Developers
- 07-23-2013, 12:11 AM #7
Re: when to use exceptions
More important even than the fact that the Java compiler does a better job optimizing simple code, is the fact that simple code is easier to understand and maintain. Following the Principle of Least Surprise is almost always more important than shaving a few milliseconds off the execution time.
Get in the habit of using standard Java naming conventions!
Similar Threads
-
exceptions
By ii88xix88ii in forum JCreatorReplies: 6Last Post: 03-12-2013, 04:07 PM -
Exceptions
By Maya in forum New To JavaReplies: 2Last Post: 05-24-2011, 07:30 AM -
Exceptions & More
By besweeet in forum New To JavaReplies: 12Last Post: 04-29-2010, 10:06 PM -
Exceptions
By hedonist in forum New To JavaReplies: 10Last Post: 09-08-2009, 09:38 AM -
Need Help With Exceptions
By maggie_2 in forum New To JavaReplies: 5Last Post: 12-15-2008, 08:12 PM
Bookmarks