• 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 - how to handle in real projects ...

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I am confuse with this two approach :



please explain me , which one is good for real projects , both point of view ( line of code , efficient ) ...

Thanks a lot .
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has virtually no impact on performance. Therefore moving to Java in General (intermediate)...
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a real project you might need different handling for different exceptions, for e.g: you'll need different handling for a NullPointerException when compared to the handling of a FileFormatException. Hence, the need for different try-catch blocks for both.

Even otherwise, it is always better to have specific exception handling before writing "catch(Exceptioon e)" that handles a generic exception, for those exceptions that you are expecting your code to generate. For other unpredictable exceptions, the generic try-catch block is good enough.

Coding exception-specific catch blocks might take some coding effort but is Good coding. This also helps when you are writing unit test cases. I do not think there is much performance overhead, as anyway only one of them would be executed.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my experience I've almost never handled two exception types differently. Generally any failure in a routine means the user is not going to get something they want, so I can catch Exception and give them the bad news. If you're really going to code different responses, you have to check the different exception types.

You also have to decide whether to throw different types just in case somebody who calls you wants to take different actions. If you always throw Exception callers may wind up trying to parse the message to decide what to do, and that's a Bad Thing.

Hope that's useful!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic