I recently read this very interesting and highly rated question about Branch prediction, and it got me thinking - how do try-catch clauses affect branch prediction (in java)? There's a lot of information out there regarding if/else, but none seem to focus on this aspect.
Does the branch predictor ever predict that an exception will be thrown? Or is it the equivalent of guessing wrong every time?
Let's put this in context. Suppose we have a List
of Strings full of numbers and words. Would the following code be very slow compared to some branch prediction optimised equivalent?
List<String> list = Arrays.asList("1", "some", "3", "words", "12", "in here");
for(String s : list){
try{
int number = Integer.parseInt(s);
// yay - it's a number, do something.
}catch(Exception e){
//this is a word - do something else.
}
}
Or is this roughly equivalent to an if(){}else{}
clause in terms of branch prediction?