Java异常处理全解析
1. 未处理的异常
在Java编程中,异常是程序运行时可能出现的错误情况。当我们运行一个程序时,如果没有对可能出现的异常进行处理,程序可能会异常终止。
例如,有一个简单的程序 ScanInt1.java ,它试图从控制台读取一个整数值并将其打印出来:
// A simple progam to accept an integer from user
import java.util.Scanner;
class ScanInt1 {
public static void main(String [] args) {
System.out.println("Type an integer in the console: ");
Scanner consoleScanner = new Scanner(System.in);
System.out.println("You typed the integer value: " + consoleScanner.nextInt());
}
}
当我们正常输入一个整数,如 10 时,程序能正确运行并输出结果:
D:\> java ScanInt1
Type an integer in the console:
10
You typed the integer value: 10
超级会员免费看
订阅专栏 解锁全文
1454

被折叠的 条评论
为什么被折叠?



