| Author |
Whats is the use of delaring SubType of Exception classes in catch clause
|
Dhineshkumar raj
Greenhorn
Joined: Aug 06, 2007
Posts: 28
|
|
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 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?? Regards, Dhinesh Kumar R
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
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 ]
|
apigee, a better way to API!
|
 |
 |
|
|
subject: Whats is the use of delaring SubType of Exception classes in catch clause
|
|
|