• Post Reply Bookmark Topic Watch Topic
  • New Topic

Query related to Design  RSS feed

 
Sat Nar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!