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;
}
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?
java spring exception-handling
marked as duplicate by DaveyDaveDave, Mark Rotteveel
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.
add a comment |
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?
java spring exception-handling
marked as duplicate by DaveyDaveDave, Mark Rotteveel
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 catchblock
. Anyway if you want your exception to be explicitly handled you should extendException
instead ofRuntimeException
– BackSlash
Nov 23 '18 at 14:46
1
What is thatAddressException
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
add a comment |
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?
java spring exception-handling
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
java spring exception-handling
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
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
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 catchblock
. Anyway if you want your exception to be explicitly handled you should extendException
instead ofRuntimeException
– BackSlash
Nov 23 '18 at 14:46
1
What is thatAddressException
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
add a comment |
1
Add another catchblock
. Anyway if you want your exception to be explicitly handled you should extendException
instead ofRuntimeException
– BackSlash
Nov 23 '18 at 14:46
1
What is thatAddressException
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
add a comment |
5 Answers
5
active
oldest
votes
Use the multi-catch block like so:
try {
stuff
} catch (AddressException | InvalidEmailAddressException ex) {
handle exception
}
add a comment |
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
}
1
CatchingException
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
add a comment |
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.
add a comment |
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");
}
add a comment |
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.
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the multi-catch block like so:
try {
stuff
} catch (AddressException | InvalidEmailAddressException ex) {
handle exception
}
add a comment |
Use the multi-catch block like so:
try {
stuff
} catch (AddressException | InvalidEmailAddressException ex) {
handle exception
}
add a comment |
Use the multi-catch block like so:
try {
stuff
} catch (AddressException | InvalidEmailAddressException ex) {
handle exception
}
Use the multi-catch block like so:
try {
stuff
} catch (AddressException | InvalidEmailAddressException ex) {
handle exception
}
answered Nov 23 '18 at 14:48
SnakeyHipsSnakeyHips
696116
696116
add a comment |
add a comment |
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
}
1
CatchingException
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
add a comment |
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
}
1
CatchingException
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
add a comment |
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
}
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
}
answered Nov 23 '18 at 14:55
GtomikaGtomika
406312
406312
1
CatchingException
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
add a comment |
1
CatchingException
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '18 at 14:55
Євген ШабалаЄвген Шабала
112110
112110
add a comment |
add a comment |
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");
}
add a comment |
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");
}
add a comment |
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");
}
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");
}
answered Nov 23 '18 at 14:55
deHaardeHaar
2,71461729
2,71461729
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '18 at 14:58
ilijev07ilijev07
1
1
add a comment |
add a comment |
1
Add another catch
block
. Anyway if you want your exception to be explicitly handled you should extendException
instead ofRuntimeException
– 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