• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Handling...What's the point?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I am a bit confused about the usefulness of exception handling...
I have seen many tutorials and books show a divide by zero example where they put the division in a try block and so on and so forth then they show that if you put zero in the denominator the exception will be caught...
Here is my question why not use if-then-else to just avoid the division by zero?
if(denominator == 0){
System.out.println("Duh...you can't do that...");
}
else{
//continue with division...
}
This is a real question that has been plaguing me for a while...
Thanks,
Nick Ueda
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, catching a divide by zero exception is not a great example since that usually indicates an error in your program logic. ArithmeticException in java, which is thrown in that situation is a RuntimeException or unchecked exception. The reason such things are unchecked is because it shouldn't happen in the first place, you need to fix your program. Now what about checked exceptions, the ones you must wrap in a try-catch block? There are many things outside the control of the programmer. For example if you are attempting to open a socket to a server somewhere on the internet, there are literally hundreds of things that can go wrong: your lan hardware could fail, your ISP could be having network problems, the server may be down for maintainence, etc. These problems are totally outside your control as a programmer. So why have an exception at all? Why not just let the connection fail? If you catch the exception you might have an oppurtunity to take an alternate course of action, like trying a different server or dialing a modem or sending an email to an administrator who can fix the problem.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or maybe logging a bunch of stuff that the class knows at that point so that some poor overworked, harrassed programmer does not get frantic trying to figure out WHY the darn exception happened to begin with.
Consider that "be kind to other programmers" programming .
[ June 23, 2003: Message edited by: Cindy Glass ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic