0

Which exception should be thrown if a file is empty?

For example,

List<Cars> cars = new LinkedList<Cars>(); 

Scanner inFile = new Scanner(new FileReader(filename));

    if(!inFile.hasNextLine()) {
        throw new ???????????????????
    } 

    while(inFile.hasNextLine()) {
        String line = inFile.nextLine(); 

        String[] CarInfo = line.split("\\|"); 

        Car tmpCar = new Car(CarInfo[0],CarInfo[1],CarInfo[2]);

        cars.add(tmpCar);

    } 
    inFile.close(); 

Thanks

Boognish
  • 103
  • 1
  • 2
  • 13
  • 1
    `IllegalArgumentException` is a possibility but you should provide also a meaningful message. – davidxxx Mar 26 '18 at 19:05
  • Why are you throwing an exception? – shmosel Mar 26 '18 at 19:06
  • 2
    I would say : none; empty input is not exceptional. – bowmore Mar 26 '18 at 19:06
  • What if the file contains a single blank line? – Jim Garrison Mar 26 '18 at 19:06
  • 1
    Drop the `if`, just go through with `nextLine`, and let users catch `NoSuchElementException`? – Sergey Kalinichenko Mar 26 '18 at 19:07
  • 2
    What are your requirements? What is the purpose of this code/method? What is expected in the file? Depending on these details, it could be `IllegalArgumentException`, `NoSuchElementException`, a custom exception, or no exception. – rgettman Mar 26 '18 at 19:10
  • I just want to throw the correct exception if the file exists but does not contain anything. Maybe I should simply create my own exception as others have noted? – Boognish Mar 26 '18 at 19:14
  • @Boognish we get that, it's just that that sounds a lot like you're using exceptions to control program flow (https://softwareengineering.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why) – bowmore Mar 26 '18 at 19:17
  • I am a student, and for my assignment, I have to throw an exception if the file exists but does not contain anything. – Boognish Mar 26 '18 at 19:18

5 Answers5

3

You can just create your own custom exception class with your own message as follow:

public class EmptyFileException extends Exception {

    private String message = "The file is empty!";

    public EmptyFileException() {
        super(message);
    }

}

Then in you code you can throw the new Exception:

Scanner inFile = new Scanner(new FileReader(filename)); 

if(!inFile.hasNextLine()) {
    throw new EmptyFileException();
}
// ...

B.

Bilal EL CHAMI
  • 414
  • 1
  • 3
  • 14
1

You could create your own exception.

class EmptyExceptoin extends Exception
    {

      public EmptyException() {}

      public EmptyException(List list)
      {
         super(list);
      }
 }

Then throwing the exception in your code:

   Scanner inFile = new Scanner(new FileReader(filename)); 

        if(!inFile.hasNextLine()) {
            throw new EmptyException();
        }
Volken
  • 43
  • 1
  • 8
0

There is no general exception from standard Java libraries for such case when the file exists but is empty.

It's look like such situation is something special for your application. So you can create your own exception type by extending Exception class. Take a look here - How to create custom exceptions in Java?

contrapost
  • 673
  • 12
  • 22
0

You have broadly two possibilities :

  • throw a RuntimeException such as IllegalArgumentException. Do it if the exception is not recoverable form the client side.

  • throw a custom checked exception if the exception has to be handled by the client. For example EmptyFileException that is public class EmptyFileException extends Exception{ }

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

Create a custom exception class that inherit from the exception class

Class EmptyFileException extends Exception{
    public EmptyFileException(){
    }
    public EmptyFileException(String customMessage){
        super(customMessage)
    }
}

You can use it within a try catch or any if statement

try
{
   //do stuff.....
}catch(Exception e){
   throw new EmptyFileException("File not found") 
}