-1

i have below piece of code which in my spring boot applicatin. This piece of code does email validation,

class EmailValidation {

    public static void validate(List<String> s){
        try {
            for (String address : s) {
                if (s == null || s.indexOf("@") < 0) {  
                    throw new InvalidEmailAddressException("Email address is invalid ");
                }
                new InternetAddress(s);
            }
        } catch(AddressException e){
            LOGGER.Error("Please validate email addresses");
        }
    }
}

class InvalidEmailAddressException extends RuntimeException {

    public InvalidEmailAddressException(String message) {
        super(message)
    }
}

My question is how do I catch InvalidEmailAddressException? How can i achieve it to handle the exception in this piece of code itself and how it will be handled by the caller?

deHaar
  • 17,687
  • 10
  • 38
  • 51
Jeena
  • 17
  • 1
  • 4
  • 1
    Add another catch `block`. Anyway if you want your exception to be explicitly handled you should extend `Exception` instead of `RuntimeException` – BackSlash Nov 23 '18 at 14:46
  • 1
    What is that ``AddressException`` that you catch? – f1sh Nov 23 '18 at 14:47
  • 2
    Your code is almost unreadable and it also does not compile. Can you please spend a minute to correct it and format it? – pleft Nov 23 '18 at 14:47

5 Answers5

2

Use the multi-catch block like so:

try { 
  stuff
} catch (AddressException | InvalidEmailAddressException ex) { 
  handle exception
}
soupjake
  • 3,293
  • 3
  • 17
  • 32
0

There're 2 types of exceptions: checked, unchecked. InvalidEmailAddressException extends RuntimeException which is unchecked exception that you shouldn't catch, so better extend it from Exception class.

yevhensh
  • 390
  • 2
  • 3
  • 14
0

As an alternative to SnakeyHips' answer, you can provide several catch blocks separately, like

try { 
    // do what you have to do here
} catch (AddressException) { 
    LOGGER.Error("AddressException thrown");
} catch (InvalidEmailAddressException ex) {
    LOGGER.Error("InvalidEmailAddressException thrown");
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
0

Firstly I think it is not good practice to extend from RuntimeException, that exception means that program crashes, you should extend from Exception. My opinion you should read some more about exceptions in Java. If your method does not catch exception you should put in method signature that method throws specific exception, something like:

public static void validate(List s) throws InvalidEmailAddressException { ... } Then make this one:

class InvalidEmailAddressException extends Exception{ public InvalidEmailAddressException(String message){ super(message) }

And that catch method, about AddressException you do not have it definition here, and I think if you want this to be proceed to caller you should not catch it at all, just declare in throws.

-1

SnakeyHips' answer is solid but be aware that you will not able to react differently to different exceptions.

try { 
   //your code
} catch (AddressException e1) { 
  //handle this exception 
} catch (InvalidAdressException e2) { 
  //handle this exception 
}

This will enable you to handle the exceptions differently. If you dont care about this you may aswell just catch the general Exception class:

try { 
   //your code
} catch (Exception e) { 
   //handle exception
}
Gtomika
  • 845
  • 7
  • 24
  • 2
    Catching `Exception` instead of specific exceptions should be a last resort; you may catch too many things that should be handled at that level. – Mark Rotteveel Nov 24 '18 at 09:00