| 
 
| 异常用在字符串转int的时候是一个很好的例子。 比如:
 public static void main(String[] args)
 {
 String s = "123a";
 if(fun(s))
 {
 System.out.println(s + "是一个整数");
 }
 else
 {
 System.out.println(s + "不是整数");
 }
 }
 public static boolean fun(String s)
 {
 int temp=0;
 try//用try、catch检查是否会抛出NumberFormatException
 {
 temp = Integer.parseInt(s);
 }
 catch (NumberFormatException e)
 {
 return false;//如果抛出返回false
 }
 return true;//否则返回真。
 }
 | 
 |