3

For example I've this file that should be formated like this:

PersonName
2,5,6,7,8,9

First line only has a name and from the second line onwards its all comma separated values. Ending in a new line. For some reason I can't make a new empty line in code format.

Now lets say that those comma separated values will be copied into an ArrayList and then I check if the ArrayList is empty or not. If it is empty I've to throw an exception. My question is, what exception to throw here?

Something like (in pseudo code):

FileReader fr = new FileReader(f);
BufferedReader buffReader = new BufferedReader(fr);

ArrayList list = new ArrayList();

while(interator.hasCSV) {
// copy values to list
    Object obj = interator.next();
    list.add(obj);
}

if(list.isEmpty()) {
   // What exception to throw here?
   // It means there was a name in the file (first line) but then no csv values 
}

I hope I made myself clear otherwise just let me know and I'll try to explain it better.

dazito
  • 7,740
  • 15
  • 75
  • 117

4 Answers4

4

as paxdiablo said you can create your own exception here is an example link

class CheckListException extends Exception
{
      //Parameterless Constructor
      public CheckListException() {}

      //Constructor that accepts a message
      public CheckListException(List list)
      {
         super(list);
      }
 }

to use the exception

 try
 {
    if(list.isEmpty()) {
         // What exception to throw here?
         // It means there was a name in the file (first line) but then no csv values 
          throw new CheckListException();
    }
 }
 catch(CheckListException ex)
 {
      //Process message however you would like
 }
Community
  • 1
  • 1
Ker p pag
  • 1,568
  • 12
  • 25
2

There's no requirement to use the standard ones defined in Java since you can create whatever ones you need.

While it looks like (from here) the closest one in the standard set is probably DataFormatException (see here), that's targeted to ZIP files.

Perhaps a better match would be ParseException. If you wanted to use one of the more targeted standard ones, that would be the one I'd be looking to use (if you don't wish to create your own), or inherit from (if you do).

But keep in mind you can always inherit from the top-level Exception class if you're not overly concerned about where it is in the hierarchy.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

Create your own exception. For further details follow this

if()//your condition
  /*create custom exeception*/
  throw new Exception("My custom exception");
}

But this is one time exception.

You have to follow the above link to create a new class for your custom exception to reuse.

Community
  • 1
  • 1
Nabin
  • 11,216
  • 8
  • 63
  • 98
2

It would be normal to place all the code responsible for parsing the content of the file in its own method. Most parsers interleave reading from the input stream and examining what it has read. Such a method must declare that it throws IOException, because the parts of the method that read from the input stream can throw an IOException. The parsing method will be easier to use if parse errors are also indicated by throwing an IOException: a caller of the method can use one catch to handle all problems, if they are not interested in the details of the problems.

A caller of the method might however want to be able to distinguish between different kinds of problems. The caller might want to report different error messages for a missing file and for an incorrectly formatted file. To support this, you could create your own InvalidFormatException that extends IOException and throw that when there is a parse error.

Raedwald
  • 46,613
  • 43
  • 151
  • 237