• Post Reply Bookmark Topic Watch Topic
  • New Topic

Whats is the use of delaring SubType of Exception classes in catch clause  RSS feed

 
Dhineshkumar raj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just go through the following code.

String ip="127.0.0.1";
StringTokenizer st = new StringTokenizer(ip,".");
int i=0,
while(st.hasMoreTokens()){
String str = st.nextToken();
try {
i=Integer.parseInt(str);
} catch (NumberFormatException e) {
System.out.println(e);
}

In the above code am using the NumberFormatException to catch the exception. But, i can also use the super class Exception also in the catch clause, thats okay.
Is there any reason or advantage of using the particular exception class like (NumberFormatException) instead of using super class (Exception) in the catch clause.

Regards,
Dhinesh Kumar R
 
Parham Emami
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reasons are:
1- You better have more specific exception types in order to handle different scenarios more efficiently. Imagine you wish your class to take different actions for each type of exceptions.

2- It is easy-to-read for your other coleagues; It'll be easier to maintain for the next poor guy debugging your code. {Sun estimates that over the lifetime of a standard piece of code, 20 percent of the effort will go into the original creation and testing of the code, and 80 percent of the effort will go into the subsequent maintenance and enhancement of the code.}

3- It has just the same nature as the case to have a general class doing various sort of stuff by having hundreds of methods (just like structured programming languages) instead of having classes doing that class-specific jobs which are high cohesion ones. That's the reason you might define your own user-specific exception classes.

Conclusion: writing a code working just fine is NOT the whole idea, writing a code presenting your good style applying good OO patterns is the best practice.

Cheers
Parham
 
Jilesh Lakhani
Ranch Hand
Posts: 49
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its more of style / pattern you follow...

1) catch(Exception)
2) catch(NumberFormatException)

the Option 1 will catch all the exceptions you encountered.. which you've never thought of.

where in case of Option 2 you can do specific things in there..
Like If I've a program which requires Input as Numeric and I've received an non-numeric character then I can be sure.. that the user has sent an Non-numeric Character due to which my program execution was terminated... and I can show the user the Specific Error Message "Errr, you were Required to input Numeric character"

and If in the above scenario for Option 1, there can be chances of getting exception other then NumberFormatException... so, you are not 100% sure of what has happen there.. though you can log it and check that....

So, its just the preference of coding style!!
that you are coding just to make sure things work.. or you are coding to make things work in best possible way and in right fashion..

Cheers!!
 
Heiko Graf
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also catch both:

} catch (NumberFormatException e) {
System.out.println("Not a number!");
} catch (Exception e) {
System.out.println("Something I realy did not think of!");
}

Just be sure to catch in the right order.

Cheers
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!