• 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

Query related to Design

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i have a small query.Kindly find the below code.

public static void handleException(throwable ex)
{
if(ex instanceof NamingException)
{
showError("NamingException");//showError is a method
}
if(ex instanceof SQLException)
{
showError("SQLException");//showError is a method
}
if(ex instanceof IOException)
{
showError("IOException");//showError is a method
}
//goes on
}

My doubt is how can i avoid these many If conditions?Is there any pattern for this or is there any better way to implement this?Kindly let me know.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you simply catch an Exception and print out its details? You can probably get something like e.getClass().getName();

CR
 
reply
    Bookmark Topic Watch Topic
  • New Topic