Wednesday, July 3, 2024
HomeLanguagesJavaScanner ioException() method in Java with Examples

Scanner ioException() method in Java with Examples

The ioException() method of java.util.Scanner class returns the IOException last thrown by this Scanner’s underlying Readable. This method returns null if no such exception exists.

Syntax:

public IOException ioException()

Return Value: This function returns the last exception thrown by this scanner’s readable.

Below programs illustrate the above function:

Program 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();
    }
}


Output:

gfg geeks!
null

Program 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();
    }
}


Output:

gopal dave!
null

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#ioException()

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments