下面程序的运行结果是( ) public class Demo { public static void main(String[] args) { try { System.out.println(10 / 0); } catch (RuntimeException e) { System.out.println("RuntimeException"); } catch (ArithmeticException e) { System.out.println("ArithmeticException"); } } }
时间: 2024-02-14 08:32:58 浏览: 82
答案是 "ArithmeticException"。在代码中,我们首先抛出了一个算术异常,然后在catch块中捕获它。因为ArithmeticException是RuntimeException的子类,所以我们必须首先匹配ArithmeticException处理程序,否则它将被RuntimeException处理程序捕获。因此,程序将打印出"ArithmeticException"。
相关问题
public class demo { public static void main(String [] args) { FileInputStream fileInputStream = null; try { System.out.println("File is openning..."); fileInputStream = new FileInputStream(new File("C:\\Users\\86132\\Desktop\\不知道\\java\\src\\TEXT1\\dwad.dat")); System.out.println("File operate successfully"); } catch(Exception e) { System.out.println("Open file failed"); } finally { System.out.println("File is closing..."); try { if(fileInputStream != null) { fileInputStream.close(); System.out.println("File is closed"); } } catch(IOException e) { System.out.println("Close file failed"); } } } } 如果dawa.dat文件已经存在,那么会输出什么
如果 `dawa.dat` 文件已经存在,则程序会输出以下内容:
```
File is openning...
File operate successfully
File is closing...
File is closed
```
这是因为程序成功打开了 `dawa.dat` 文件,所以会输出 `File operate successfully`,然后在 `finally` 块中关闭了文件,并输出 `File is closing...` 和 `File is closed`。
class Main { static void procedure() throws IllegalAccessException { System.out.println("inside procedure"); IllegalAccessException("demo"); } public static void main(String args[]) { try { procedure(); } catch ( ) { System.out.println("捕获:" + e); } } }
这段代码有一些问题,比如:
1. 在 `IllegalAccessException` 抛出的地方缺少 `new` 关键字,应该是 `new IllegalAccessException("demo")`。
2. 在 `catch` 块中缺少异常类型,应该是 `catch (IllegalAccessException e)`。
3. 缺少异常处理的代码,应该在 `catch` 块中加入处理异常的代码,比如输出异常信息或者进行其他处理。
改正后的代码如下:
```
class Main {
static void procedure() throws IllegalAccessException {
System.out.println("inside procedure");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
procedure();
} catch (IllegalAccessException e) {
System.out.println("捕获:" + e);
}
}
}
```
阅读全文