作业帮 > 综合 > 作业

下面这段程序看不懂 谁能大概解释下的

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/09 09:22:27
下面这段程序看不懂 谁能大概解释下的
import java.io.*;
class J_Exception extends Exception {
} public class J_Test {
public static void main(String[] args)
{ try { int i = System.in.read(); if(i == '0')
throw new J_Exception(); //手动抛出异常
System.out.print("1");
}catch(IOException ex) {
System.out.print("2");
}catch(J_Exception ex) {
System.out.print("3");
}finally
{ System.out.println("4");
}
}
}
为什么当输入0时得到34
你这不是写的很清楚了么.

class J_Exception extends Exception {

}

public class J_Test {
public static void main(String[] args) {
try {
int i = System.in.read();
if (i == '0') // 因为你输入的是0,所以这个判断为真,抛出下面的异常
throw new J_Exception(); // 手动抛出异常
System.out.print("1");
} catch (IOException ex) {
System.out.print("2");
} catch (J_Exception ex) { // 在这里捕获异常,所以输出3
System.out.print("3");
} finally {
System.out.println("4"); // 这句一定会被执行, 所以输出4 
}
}
}
再问: 额 刚学的这个 不懂 呵呵
再答: 这回懂了不?