首页 > 试题广场 >

阅读以下程序并写出运行结果:()

[填空题]
阅读以下程序并写出运行结果:1
public class ExceptionTest {
static String a[]={"123","abc",null};
public static void main (String args[]) {
for (int i = 0; i < 3; i++) {
try {
int x = Integer.parseInt(a[i]);
System.out.print(  "Result: " + x + " ");
}
catch(NullPointerException e) {
System.out.print("error null:" + " ");
}
catch (NumberFormatException e){
System.out.print("error :abc"+" ");
}
finally{
System.out.print ("In "+ i +"th loop" + " ");
}
}
}
}

Integer.parseInt()方法只会抛出NumberFormatException

parseInt()源码如下:
    public static int parseInt(String s, int radix)
                throws NumberFormatException
    {
        /*
         * WARNING: This method may be invoked early during VM initialization
         * before IntegerCache is initialized. Care must be taken to not use
         * the valueOf method.
         */

        if (s == null) {
            throw new NumberFormatException("null");
        }

        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " less than Character.MIN_RADIX");
        }

        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " greater than Character.MAX_RADIX");
        }

        int result = 0;
        boolean negative = false;
        int i = 0, len = s.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;

        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                } else if (firstChar != '+')
                    throw NumberFormatException.forInputString(s);

                if (len == 1) // Cannot have lone "+" or "-"
                    throw NumberFormatException.forInputString(s);
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++),radix);
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        return negative ? result : -result;
    }

发表于 2017-05-13 21:23:45 回复(0)
。。。。这题有毒
发表于 2017-05-09 17:34:28 回复(0)
为什么我的运行结果是这样:

发表于 2017-05-09 21:53:41 回复(7)
Integer.parseInt()方法只会抛出NumberFormatException
发表于 2017-06-22 09:50:35 回复(0)
***正确率的题,直接pass
发表于 2017-06-01 21:29:41 回复(0)
总结:在try中没有异常的情况下try、catch、finally的执行顺序 try --- finally
如果try中有异常,执行顺序是try --- catch --- finally
如果try中没有异常并且try中有return这时候正常执行顺序是try ---- finally --- return
如果try中有异常并且try中有return这时候正常执行顺序是try ---- catch  ---- finally --- return
如果在try和catch中没有执行System.exit();那么 finally 永远执行!
发表于 2017-07-17 10:54:04 回复(0)

?
发表于 2017-07-14 14:57:52 回复(0)
Integer jdk源码
if (s == null) { throw new NumberFormatException("null");
}

发表于 2017-07-10 19:04:23 回复(0)
Integer.parseInt(String s) 只有 NumberFormatException只抛出这一种异常
发表于 2017-07-07 12:05:27 回复(0)
呵呵

发表于 2017-07-04 12:05:31 回复(1)
 被这些题搞得微醺
发表于 2017-06-22 16:15:34 回复(0)
英文冒号变中文冒号可以的
发表于 2017-06-19 09:59:43 回复(0)
 我的想法是:Result: 123 in 0 th loop Result:abc error:abc in 1 th loop, 请问一下不是出现异常,程序终止吗?咋回事呢?
发表于 2017-06-08 14:30:09 回复(1)
使用println输出,我是不愿意回答的。
发表于 2017-05-31 16:06:34 回复(0)
居然不能换行, 然后我自己加了换行符, ...................
Result:123\nln 0th loop\n\nerror : abc\nln 1th loop\n\nerror null : ln 2th loop\n\n

发表于 2017-05-30 00:36:23 回复(0)
这个题目 注意 finally语句块在 for循环之中,finally语句块特性 不管 try语句块正常结束还是异常结束 finally 语句块还是需要执行的,因此如果将 System语句换做 return语句 那么 try中return 被finally语句块中return覆盖。
 Result:123 In 0th loop error:abc In 1th loop error :abc In 2th loop.
发表于 2017-05-20 10:49:54 回复(0)
可以考虑把答案的空格去掉啊。。。
发表于 2017-05-09 09:22:32 回复(1)
做对的可以考虑去买彩票
发表于 2017-05-08 22:02:40 回复(0)