Supersoaker
Supersoaker

Reputation: 61

Custom Exception-Java

Well, i'm programming a rent car system with clients and these have an id card. At the time of renting the car the client needs to identify himself with the id, so I need a custom exception that handles if the input of the user is 8 numbers and one letter, e.g.:

55550000A

I've already made an exception for if the input is int or not and it goes:

   import java.util.*;
   import java.util.Scanner;
public class read {
static Scanner leer=new Scanner(System.in);
public static int readInt() {
    int num = 0;
    boolean loop = true;

    while (loop) {
        try {
            num = leer.nextInt();
            loop = false;
        } catch (InputMismatchException e) {
            System.out.println("Invalid value!");
            System.out.println("Write again");
    leer.next();
         } 
      }
    return num;
  }
}

The only thing you have to do is declare the variable and call the method like this:

int variable=read.readInt();

So it would be good if the id could work like that, I mean another method readId() which would return the value. The thing is that i don't know how to make an exception for a custom format or if this is even possible so any help will be helpful. Thank you very much!

Upvotes: 1

Views: 1234

Answers (4)

frunkad
frunkad

Reputation: 2543

Your question was a little confusive but I guess you want to create a new exception.

Create a file MyAppException.java

class MyAppException extends Exception {

private String message = null;

public MyAppException() {
    super();
}

public MyAppException(String message) {
    super(message);
    this.message = message;
}
}

You can throw it via

throw new MyAppException();

But I guess an exception is not needed for what you want:

public static String readId() {
    String id = "";
    while(true){
        id = leer.next();
        try{
            Integer.parseInt(id.substring(0,8));
        }catch(InputMismatchException e){
            System.out.println("Invalid Input");
            continue;
        }
        if(id.length() != 9)continue;
        if(Character.isLetter(id.chatAt(8)))break;
    }
    return id;
}

Upvotes: 1

Razib
Razib

Reputation: 11173

Create your own exception by extending Exception -

class InvalidIdException extends Exception
{
      public InvalidIdException() {}

      public InvalidIdException(String message)
      {
         super(message);
      }
 }

And from your client class check whether the input id is valid or not. If it is an invalid id then throw the InvalidIdException. Suppose your are validating your id in the validateId() method. Then you can throw an InvalidIdException from this method -

boolean isValidId(String id) throws InvalidIdException{

    if(id == null){
            throw new InvalidIdException("Id is null");
        }
   else {
    //check for id formatting 
    //for example whether id has it's minimum length
    //contains any character etc.
    throw new InvalidIdException("Id has wrong format");
   }

   return true;
}

Upvotes: 0

Evan Bechtol
Evan Bechtol

Reputation: 2865

Typically, you only want to throw exceptions to prevent some kind of error from causing your program to completely fail in some form. A better solution would be to validate input before it is used later in the program. Create conditions that check if the input is the desired length and data type, if it is not, you need to get the input again.

Using exceptions excessively points to bad design and the potential for serious problems if the code is modified later on.

Upvotes: 0

cyber_rookie
cyber_rookie

Reputation: 665

@epascarello is correct, other than the Java in their names, they are both very different, as for the question, try to make a custom exception like this:

public class InputMismatchException extends Exception {
    public InputMismatchException(String message) {
        super(message);
    }
}

Upvotes: 0

Related Questions