Though the question is generic, I would mention the scenario which sparked the query.
Scenario:
I am interested in analyzing a large number of strings (numeric ones in particular). Therefore, my first job is to filter out those ones which contain even a single character other than numbers.
A simple way to do this is (in Java):
for (String val : stringArray){
try{
int num = Integer.parseInt(val);
doSomething(num);
}
catch(NumberFormatException nfe){}
}
Another point which I must mention is that there are only about 5% of the strings in the array which are purely numeric. Thus there would be, in short, a lot of catching involved.
What I was wondering about was that whether this was an efficient way in terms of design or should I be thinking of other ways to do the same?
Conclusion based on answers: Exceptions are indeed expensive and it is not a very good design practice to use them as a form of control statement. Therefore, one should try and look for alternatives wherever possible and if still exceptions seem to be clearer/easier, one should document it well.