How is multiple exception handling done in java? [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1
















This question already has an answer here:




  • Exception Handling with Multiple catch block [duplicate]

    2 answers



  • Can I catch multiple Java exceptions in the same catch clause?

    9 answers




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?










share|improve this question















marked as duplicate by DaveyDaveDave, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 8:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 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


















-1
















This question already has an answer here:




  • Exception Handling with Multiple catch block [duplicate]

    2 answers



  • Can I catch multiple Java exceptions in the same catch clause?

    9 answers




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?










share|improve this question















marked as duplicate by DaveyDaveDave, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 8:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 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














-1












-1








-1









This question already has an answer here:




  • Exception Handling with Multiple catch block [duplicate]

    2 answers



  • Can I catch multiple Java exceptions in the same catch clause?

    9 answers




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?










share|improve this question

















This question already has an answer here:




  • Exception Handling with Multiple catch block [duplicate]

    2 answers



  • Can I catch multiple Java exceptions in the same catch clause?

    9 answers




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?





This question already has an answer here:




  • Exception Handling with Multiple catch block [duplicate]

    2 answers



  • Can I catch multiple Java exceptions in the same catch clause?

    9 answers








java spring exception-handling






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 14:51









deHaar

2,71461729




2,71461729










asked Nov 23 '18 at 14:44









JeenaJeena

72




72




marked as duplicate by DaveyDaveDave, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 8:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by DaveyDaveDave, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 8:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 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














  • 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








1




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





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




1





What is that AddressException that you catch?

– f1sh
Nov 23 '18 at 14:47





What is that AddressException that you catch?

– f1sh
Nov 23 '18 at 14:47




2




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





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 Answers
5






active

oldest

votes


















1














Use the multi-catch block like so:



try { 
stuff
} catch (AddressException | InvalidEmailAddressException ex) {
handle exception
}





share|improve this answer































    0














    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
    }





    share|improve this answer



















    • 1





      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 9:00



















    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.






    share|improve this answer































      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");
      }





      share|improve this answer































        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.






        share|improve this answer






























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Use the multi-catch block like so:



          try { 
          stuff
          } catch (AddressException | InvalidEmailAddressException ex) {
          handle exception
          }





          share|improve this answer




























            1














            Use the multi-catch block like so:



            try { 
            stuff
            } catch (AddressException | InvalidEmailAddressException ex) {
            handle exception
            }





            share|improve this answer


























              1












              1








              1







              Use the multi-catch block like so:



              try { 
              stuff
              } catch (AddressException | InvalidEmailAddressException ex) {
              handle exception
              }





              share|improve this answer













              Use the multi-catch block like so:



              try { 
              stuff
              } catch (AddressException | InvalidEmailAddressException ex) {
              handle exception
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 '18 at 14:48









              SnakeyHipsSnakeyHips

              696116




              696116

























                  0














                  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
                  }





                  share|improve this answer



















                  • 1





                    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 9:00
















                  0














                  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
                  }





                  share|improve this answer



















                  • 1





                    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 9:00














                  0












                  0








                  0







                  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
                  }





                  share|improve this answer













                  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
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 14:55









                  GtomikaGtomika

                  406312




                  406312








                  • 1





                    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 9:00














                  • 1





                    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 9:00








                  1




                  1





                  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 9:00





                  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 9:00











                  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.






                  share|improve this answer




























                    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.






                    share|improve this answer


























                      0












                      0








                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 23 '18 at 14:55









                      Євген ШабалаЄвген Шабала

                      112110




                      112110























                          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");
                          }





                          share|improve this answer




























                            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");
                            }





                            share|improve this answer


























                              0












                              0








                              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");
                              }





                              share|improve this answer













                              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");
                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 23 '18 at 14:55









                              deHaardeHaar

                              2,71461729




                              2,71461729























                                  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.






                                  share|improve this answer




























                                    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.






                                    share|improve this answer


























                                      0












                                      0








                                      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.






                                      share|improve this answer













                                      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.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 23 '18 at 14:58









                                      ilijev07ilijev07

                                      1




                                      1















                                          這個網誌中的熱門文章

                                          Hercules Kyvelos

                                          Tangent Lines Diagram Along Smooth Curve

                                          Yusuf al-Mu'taman ibn Hud