带有示例的Java中的扫描仪 ioException() 方法
Java.util.Scanner类的ioException()方法返回此 Scanner 的底层 Readable 最后抛出的IOException 。如果不存在此类异常,则此方法返回 null。
句法:
public IOException ioException()
返回值:此函数返回此扫描仪可读的最后一次抛出的异常。
下面的程序说明了上述函数:
方案一:
// Java program to illustrate the
// ioException() method of Scanner class in Java
// without parameter
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "gfg geeks!";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// print the line
System.out.println("" + scanner.nextLine());
// check if there is an IO exception
System.out.println("" + scanner.ioException());
// close the scanner
scanner.close();
}
}
输出:
gfg geeks!
null
方案二:
// Java program to illustrate the
// ioException() method of Scanner class in Java
// without parameter
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "gopal dave!";
// new scanner with the specified String Object
Scanner scanner = new Scanner(s);
// print the line
System.out.println("" + scanner.nextLine());
// checks if there is an IO exception
System.out.println("" + scanner.ioException());
// close the scanner
scanner.close();
}
}
输出:
gopal dave!
null
参考: https: Java/util/Scanner.html#ioException()