Second one is better, since you have if
/ else if
statements, only one of them will be executed, and might throw an exception.So, putting the statement into a try/catch
is more logical.In this case I can't see any advantage of using first version over to second.Your second code snippet does the same job with less code.
However, if you want to handle different exceptions on each statement then you will have to use different try
/ catch
statements.