Java Scanner ioException()方法及示例

来源:这里教程网 时间:2026-02-17 21:08:12 作者:

Java Scanner ioException()方法及示例

java.util.Scanner 类的 ioException() 方法返回该Scanner的底层Readable最后抛出的IOException。如果不存在这样的异常,该方法返回null。

语法

public IOException ioException()

返回值: 该函数返回该扫描器的可读性所抛出的最后一个异常。

下面的程序说明了上述函数。

程序1 :

// 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

程序2

// 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://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#ioException()

相关推荐