Try:
throw new Exception("transction: " + transNbr, E);
Answer from Boris Pavlović on Stack OverflowTry:
throw new Exception("transction: " + transNbr, E);
Exceptions are usually immutable: you can't change their message after they've been created. What you can do, though, is chain exceptions:
throw new TransactionProblemException(transNbr, originalException);
The stack trace will look like
TransactionProblemException : transNbr
at ...
at ...
caused by OriginalException ...
at ...
at ...
Videos
Most standard exception classes provide a constructor that takes a mesage, for example:
public UnsupportedOperationException(String message) {
super(message);
}
The above class simply calls its parent's constructor, which calls its parent's constructor, and so on, ultimately culminating in:
public Throwable(String message) {
...
}
If you create your own exception class, I think it's a good idea to following this convention.
You can only set the message at the creation of the exception. Here is an example if you want to set it after the creation.
public class BusinessException extends RuntimeException{
private Collection<String> messages;
public BusinessException(String msg){
super(msg);
}
public BusinessException(String msg, Exception cause){
super(msg, cause);
}
public BusinessException(Collection<String> messages){
super();
this.messages= messages;
}
public BusinessException (Collection<String> messages, Exception cause){
super(cause);
this.messages= messages;
}
@Override
public String getMessage(){
String msg;
if(this.messages!=null && !this.messages.isEmpty()){
msg="[";
for(String message : this.messages){
msg+=message+",";
}
msg= StringUtils.removeEnd(msg, ",")+"]";
}else msg= super.getMessage();
return msg;
}
}
Is it bad practice to throw multiple custom exceptions in Java?
No. It is good practice.
The only situation where multiple exceptions might be a (slightly) bad idea is if there is absolutely no possibility (ever!) of catching individual exceptions; i.e. the fine-grained exceptions won't serve any functional purpose. However, my opinion is that if you can make that assertion, then there is probably something wrong with the way that you / your team is using Java. And the counter-argument is that sensible use of multiple custom exceptions will help you to document the APIs ... even if you are never going to do more than catch-log-and-bail-out at runtime.
This is not to say that lots of custom exceptions will always be good:
If you go overboard and create a separate exception for everything, then you are probably adding unnecessary complexity. (In a lot of cases, different exception messages are sufficient.)
If you don't have a sensible inheritance hierarchy for your custom exceptions, then you may end up regretting it. (A well-designed hierarchy allows you to catch "classes" of exceptions, or declare methods as throwing them. It can make your code simpler.)
Is it bad practice to throw multiple custom exceptions in Java?
Generally speaking: No. Why should it?
As with everything: Abstraction is your friend.
Is it necessarry to have a CustomerNotFound Exception and a ProductNotFound Exception? Or are your requirements just a more abstract NotFoundException?
The context could help to determine, what was missing. Having different exceptions for the sake of having them is nonsense.
Is it necessary, each layer of your application having custom exceptions? Exceptions are a way to report, that an intended action failed due to some reason.
Say, you have a controller which asks the service-layer to retrieve data, which in turn asks the DA-layer to read values from the DB. The resultset is empty. The service gets the empty resultset and throws a
NotFoundExceptionthe service communicates, the failure of the action due to a missing result.Say, the controller needs the service to do the payrolls for employees. And the service is asked to do the payroll for the employee with ID
123456, and in turn asks a service to retrieve the employee - but no emloyee could be found.
There are two ways to deal with that:
1) You throw a NotFound exception in the DA-Layer, catch it in the payroll-service and rethrow a PayrollServiceException wrapping the NotFoundException with the message Exmployee could not be found
2) You throw a NotFound exception in the DA-Layer and do not catch it in the payroll service and catch it instead a layer above.
I would go for (2), since in (1) the information, that the action failed because of a missing employee is redundant.
In addition to Eran's answer, you could also make your custom Exception extend RuntimeException, which does not need to be caught.
If the method that throws this exception doesn't handle it (i.e. it doesn't catch it), it must declare it in the throws clause, since this is a checked exception.
public void yourMethod () throws UserException
{
...
throw new UserException("Something failed.", new Throwable(String.valueOf(UserExceptionType.CaptureFailed)));
...
}