try catch使用方法
class Solution {
public int reverse ( int x){
int i;
if (x > 0) {
String s = Integer.toString(x);
StringBuffer s1 = new StringBuffer(s).reverse();
String s2 = s1.toString();
try { //s2必须是字符串才可以抛出异常
i = Integer.parseInt(s2);
} catch (NumberFormatException e) {
return 0;
}
int a = Integer.valueOf(s2);
return a;
}
if (x < 0) {
x = 0 - x;
String s = Integer.toString(x);
StringBuffer s1 = new StringBuffer(s).reverse();
String s2 = s1.toString();
try {
i = Integer.parseInt(s2);
} catch (NumberFormatException e) {
return 0;
}
int a = Integer.valueOf(s2);
int b = 0 - a;
return b;
}
return 0;
}
}
}