• 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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Just go through the following code.

String ip="";
StringTokenizer st = new StringTokenizer(ip,".");
int i=0,
String str = st.nextToken();
try {
} catch (NumberFormatException 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 reason or advantage of using the particular class say (NumberFormatException) instead of super class (Exception) in the catch clause.
Is there any reasons like saving the compilation time.

For example,

There is an diff' between wild card import and ordinary import.

2 types of import statements

1) import java.util.*;
2) import java.util.ArrayList;

Here 1st type is wild card import, In this the compiler will import all the classes in that package. But am using only ArrayList in my program. So there is waste of compilation time here. Its is not efficient to use wild card import statement.

Like this any reason is there while declaring exception class in catch clause??

Dhinesh Kumar R
Nitesh Kant
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly a few mantras on
how to ask questions on javaranch:
  • This is not the correct forum for this post. ( CarefullyChooseOneForum)
  • Use code tags while posting code. (UseCodeTags)

  • Coming to your question:
    No the intention of catching a specific type of exception is simple: You want to perform a specific action *only* when that exception occurs.
    Its the same thing as saying "dude there is *something* wrong in your input" as opposed to saying "dude you need to enter a number here and not your name" !! You can only do this when either you catch NumberFormatException or check instance of the Exception caught (This will make people looking at the code crazy and put a price on your head!) and then take different actions.
    [ February 26, 2008: Message edited by: Nitesh Kant ]
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!