package TestException;
/*
* 常见异常:
* RuntimeException子类异常:
* 1. ArithmeticException 算数异常
* 2. NullPointerException 空指针异常
* 3. InputMismatchException 输入异常
* 4. ArrayIndexOutOfBoundsException 数组下标越界
* 非RuntimeException子类异常:
* 5. IOException //(必须处理的异常,不然编译报错)非RuntimeException子类异常
*/
import java.util.*;//Scanner类和输入异常存放在java.util包中,故需要导入才可以使用
public class TestExcep_2 {
static int i;//因为属性i在静态方法中使用,静态方法不能调用非静态成员,所以i必须为静态的。非静态属性和方法在类实例化时才会创建。
public static void main(String[] args) {
// TODO Auto-generated method stub
//int i;
Scanner sc = new Scanner(System.in);//System.in表示键盘输入
try
{
while(sc.hasNext())//使用hasNext方法
{
i = sc.nextInt();
System.out.println(i);
}
}
catch (InputMismatchException e){
System.out.println("输入不合法,抛出异常!");
return ;//即使接收到异常,程序在此处也不会结束,因为finally语句块一定会执行
}
finally{
System.out.println("finally");
}
}
}
/*
* 常见异常:
* RuntimeException子类异常:
* 1. ArithmeticException 算数异常
* 2. NullPointerException 空指针异常
* 3. InputMismatchException 输入异常
* 4. ArrayIndexOutOfBoundsException 数组下标越界
* 非RuntimeException子类异常:
* 5. IOException //(必须处理的异常,不然编译报错)非RuntimeException子类异常
*/
import java.util.*;//Scanner类和输入异常存放在java.util包中,故需要导入才可以使用
public class TestExcep_2 {
static int i;//因为属性i在静态方法中使用,静态方法不能调用非静态成员,所以i必须为静态的。非静态属性和方法在类实例化时才会创建。
public static void main(String[] args) {
// TODO Auto-generated method stub
//int i;
Scanner sc = new Scanner(System.in);//System.in表示键盘输入
try
{
while(sc.hasNext())//使用hasNext方法
{
i = sc.nextInt();
System.out.println(i);
}
}
catch (InputMismatchException e){
System.out.println("输入不合法,抛出异常!");
return ;//即使接收到异常,程序在此处也不会结束,因为finally语句块一定会执行
}
finally{
System.out.println("finally");
}
}
}