I have successfully implemented a custom exception using below code
CarNotFoundException.Java
public class CarNotFoundException extends Exception
{
private static final long serialVersionUID = 1L;
public CarNotFoundException(String msg) {
super(msg);
}
}
Car.Java
public static CarProvider getInstanceByProvider(String provider) throws CarNotFoundException {
if(!provider.equals(Constants.BMW || Constants.B||Constants.C{
throw new CarNotFoundException("Car Not Found");
}
return carProvider;
}
CarTest.java
try
{
carProvider = Car.getInstanceByProvider(provider);
} catch (CarNotFoundException e) {
e.printstacktrace();
}
What I want to do ?
Instead of e.printStackTrace(); when I calle.getMessage(),
I get nothing(blank).
How I can make custom e.getMessage() ?
Edit : I got my answer, I missed System.out.println()
Thanks for helping..!
Override getMessage() method in your custom exception
public class CarNotFoundException extends Exception
{
private static final long serialVersionUID = 1L;
public String message;
public CarNotFoundException(String msg) {
this.message = msg;
}
// Overrides Exception's getMessage()
#Override
public String getMessage(){
return message;
}
}
Related
New to this topic and right now I'm stuck at a brick wall. I have 2 classes, parent class: Controller.java and subclass: GreenhouseControls.java. I need to serialize a GreenhouseControls object but also an instance variable (eventList) from its superclass Controller.java.
My serialization happens when an inner class of GreenhouseControls.java throws a custom ControllerException, which is caught in the main method. Before terminating the program, the GreenhouseControls object should be saved (including the field from its superclass).
Why is a NotSerializableException thrown by the inner class WindowMalfunction of GreenhouseControls? Anyone have any ideas, as I am seriously stuck?
What I tried is the following:
Implement serializable on Controller.java. This is because if the superclass is serializable, then subclass is automatically serializable, however this throws java.io.NotSerializableException: GreenhouseControls$WindowMalfunction, (WindowMalfunction is the inner class that throws the initial exception to begin the serialization processs).
Implement serializable on GreenhouseControls.java and implement custom serialization by overriding writeObject() and readObject() to save the field from the superclass. This approach yet again throws the same exception as the approach 1.
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(super.eventList);
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
in.defaultReadObject();
Object obj = in.readObject();
List<Event> x = cast(obj);
super.eventList = x;
}
Controller.java
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class Controller {
// THIS IS THE VARIABLE I NEED TO SAVE
protected List<Event> eventList = new ArrayList<Event>();
public void addEvent(Event c) {
eventList.add(c);
}
public void run() throws ControllerException {
while (eventList.size() > 0)
// Make a copy so you're not modifying the list
// while you're selecting the elements in it:
for (Event e : new ArrayList<Event>(eventList))
if (e.ready()) {
System.out.println(e);
e.action();
eventList.remove(e);
}
}
public static void shutDown() { }
}
GreenhouseControls.java class (note I have removed the inner classes and other code from it and only left related info)
public class GreenhouseControls extends Controller implements Serializable {
private int errorcode = 0;
public class WindowMalfunction extends Event {
public WindowMalfunction(long delayTime) {
super(delayTime);
}
public void action() throws ControllerException {
windowok = false;
throw new ControllerException("Window malfunction");
}
public String toString() {
return "Window malfunction";
}
}
public class PowerOut extends Event {
public PowerOut(long delayTime) {
super(delayTime);
}
public void action() throws ControllerException {
poweron = false;
throw new ControllerException("Power out");
}
public String toString() {
return "Power out";
}
}
// Various other inner classes that extend event exist
public static void serializeObject(GreenhouseControls gc) {
FileOutputStream fileOut;
ObjectOutputStream out;
try {
fileOut = new FileOutputStream("dump.out");
out = new ObjectOutputStream(fileOut);
out.writeObject(gc);
System.out.println("WERRROR code: " + gc.getError());
out.close();
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(super.eventList);
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
in.defaultReadObject();
Object obj = in.readObject();
List<Event> x = cast(obj);
super.eventList = x;
}
#SuppressWarnings("unchecked")
public static <T extends List<?>> T cast(Object obj) {
return (T) obj;
}
public int getError() {
return errorcode;
}
public Fixable getFixable(int errorcode) {
switch (errorcode) {
case 1:
return new FixWindow();
case 2:
return new PowerOn();
default:
return null;
}
}
public static void main(String[] args) {
GreenhouseControls gc = null;
try {
String option = args[0];
String filename = args[1];
if (!(option.equals("-f")) && !(option.equals("-d"))) {
System.out.println("Invalid option");
printUsage();
}
// gc = new GreenhouseControls();
if (option.equals("-f")) {
gc = new GreenhouseControls();
gc.addEvent(gc.new Restart(0, filename));
}
gc.run();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid number of parameters");
printUsage();
} catch (ControllerException e) {
String errormsg;
if (e.getMessage().equals("Window malfunction")) {
gc.errorcode = 1;
errormsg = "Window malfunction event occurred Error code: " + gc.errorcode;
} else {
gc.errorcode = 2;
errormsg = "Power out event occurred Error code: " + gc.errorcode;
}
logError(errormsg);
serializeObject(gc);
gc.displayEventList();
shutDown();
}
}
}
Event.java
public abstract class Event {
private long eventTime;
protected final long delayTime;
public Event(long delayTime) {
this.delayTime = delayTime;
start();
}
public void start() { // Allows restarting
eventTime = System.currentTimeMillis() + delayTime;
}
public boolean ready() {
return System.currentTimeMillis() >= eventTime;
}
public abstract void action() throws ControllerException;
Event has to be Serializable too.
Change
public abstract class Event {
to
public abstract class Event implements Serializable {
I am new to Java and trying my hands on the exception handling code. Everything was fine to me until I get unhandled exception error. Can anyone please help me to correct the code and tell my mistake so that I can never commit again?
Exception Class - Created this to retrieve message for different exceptions
// Implement user defined exception classes
class InvalidAgeException extends Exception{
public InvalidAgeException(String message) {
super(message);
}
}
class InvalidJobProfileException extends Exception{
public InvalidJobProfileException(String message) {
super(message);
}
}
class InvalidNameException extends Exception{
public InvalidNameException(String message) {
super(message);
}
}
Applicant Class - Class to set and get attributes of Applicant
class Applicant {
private String name;
private String jobProfile;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJobProfile() {
return jobProfile;
}
public void setJobProfile(String jobProfile) {
this.jobProfile = jobProfile;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Validator Class - Class to check if the Applicant has a name or not
class Validator{
//Implement your code here
public boolean validateName(String name) throws Exception
{
if(getName().length()>0)
{
return true;
}
else{
return false;
}
}
public boolean validateJobProfile(String jobProfile) throws Exception
{
if (getJobProfile().equalsIgnoreCase("Associate") || getJobProfile().equalsIgnoreCase("Clerk") ||
getJobProfile().equalsIgnoreCase("Executive") || getJobProfile().equalsIgnoreCase("Officer"))
{
return true;
}
else{
return false;
}
}
public boolean validateAge(int age) throws Exception
{
if(getAge()>=18 && getAge()<=30)
{
return true;
}
else{
return false;
}
}
public void validate(Applicant applicant) throws Exception
{
if(validateName(getName())==false)
{
throw new InvalidNameException("Invalid Name");
}
if (validateJobProfile(getJobProfile())==false)
{
throw new InvalidJobProfileException("Invalid job post");
}
if (validateAge(getAge())==false)
{
throw new InvalidAgeException("Invalid Age");
}
}
}
Tester Class - Main Class where objects of different classes are created
class Tester {
public static void main(String[] args) {
try {
Applicant applicant= new Applicant();
applicant.setName("Jenny");
applicant.setJobProfile("Clerk");
applicant.setAge(25);
Validator validator = new Validator();
validator.validate(applicant);
System.out.println("Application submitted successfully!");
}
catch (InvalidNameException|InvalidJobProfileException|InvalidAgeException e) {
System.out.println(e.getMessage());
}
}
}
Your method declares that it throws Exception. Thus, you have to actually catch Exception. If you only want to have to catch either of the three custom exceptions, you need to declare your method as only throwing those three via throws InvalidNameException, InvalidJobProfileException, InvalidAgeException
Plus, your validateAge is declared as throwing an exception, but never actually does throw anything.
Your methods need to specify which exceptions they are actually throwing. At the moment you are simply writing that they throw the general Exception, which you then don't catch in your main.
Change
public void validate(Applicant applicant) throws Exception{...}
to
public void validate(Applicant applicant) throws InvalidNameException, InvalidJobProfileException, InvalidAgeException{...}
For the other methods you need to do it similarly.
The context is:
I'm making a upload csv service and when i do some verification on the content i want to throw a custom runtime exception who contain the error message and the line to the problem.
What i got for now is:
public class NotValidCsvException extends RuntimeException {
private static final long serialVersionUID = 1L;
private Integer _line;
public NotValidCsvException(final String message, final Integer line) {
super(formatMessage(message, line));
}
public static String formatMessage(String message, Integer line) {
return new StringBuilder()
.append("{message: ")
.append("'")
.append(message)
.append("'")
.append(", ligne: ")
.append("'")
.append(line)
.append("'")
.append("}").toString();
}
}
But the problem is that i got a response like:
{"timestamp":1511785651810,"status":500,"error":"Internal Server Error","exception":"com.sstrn.pa.service.impl.exception.NotValidCsvException","message":"{"message": "message_import_csv_undefined_thing", "line": 0}"}
But i'd like to have something like:
{"timestamp":1511785651810,"status":500,"error":"Internal Server Error","exception":"com.sstrn.pa.service.impl.exception.NotValidCsvException","message":"message_import_csv_undefined_thing", "line": 0}
So to do this i created and interface AttributeLineEnabled whose implemented by my NotValidCsvException.
public interface AttributeLineEnabled {
public Integer getLine();
}
public class NotValidCsvException extends RuntimeException implements AttributeLineEnabled {
private static final long serialVersionUID = 1L;
private Integer _line;
public NotValidCsvException(final String message) {
super(message);
}
public NotValidCsvException(final String message, Integer line) {
super(message);
_line = line;
}
public Integer getLine() {
return _line;
}
public void setLine(final Integer line) {
_line = line;
}
}
Then i created a controller who extends DefaultErrorAttributes and implement ErrorAttributes.
This controller override the getErrorAttributes method who set the jsonResponse.
public class BaseErrorAttributes extends DefaultErrorAttributes implements ErrorAttributes {
private static final String LINE = "line";
public BaseErrorAttributes() {
super();
}
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
Throwable error = getError(requestAttributes);
if (error instanceof AttributeLineEnabled) {
AttributeLineEnabled attributeLineEnabled= (AttributeLineEnabled) error;
errorAttributes.put(LINE, attributeLineEnabled.getLine());
}
return errorAttributes;
}
}
And i inject it into my controller who make the importation process:
private BaseErrorAttributes _baseErrorAttributes;
// getter and setter too
I have created a new exception class in my Dropwizard service that extends BadRequestException.
public class CustomBadRequestException extends BadRequestException {
private static final long serialVersionUID = 1L;
private List<ValidationFailureDto> validationFailures;
public CustomBadRequestException() {
super();
}
public CustomBadRequestException(final List<ValidationFailureDto> validationFailures) {
super();
this.validationFailures = validationFailures;
}
#ApiModelProperty(value = "List of validationFailures")
public List<ValidationFailureDto> getValidationFailures() {
return validationFailures;
}
}
When I throw that exception at first I was only getting back the deserialised BadRequestException, minus the additional property (validationFailures)
{
code: "400",
message: "Bad request"
}
This is because Dropwizard's internals have a default exception mapper that allows Jetty/Jackson to understand domain exceptions and how to send the appropriate HTTP response.
To overcome this you can implement your own ExceptionMapper class and register it with Dropwizard.
public class CustomBadRequestExceptionMapper implements ExceptionMapper<SamplePackOrderBadRequestException> {
/**
* Allows jackson to deserialise custom exceptions and its properties to JSON response
*
* #param exception exception
* #return response object
*/
#Override
public Response toResponse(final SamplePackOrderBadRequestException exception) {
if (exception instanceof SamplePackOrderBadRequestException) {
SamplePackOrderBadRequestException samplePackOrderBadRequestException
= (SamplePackOrderBadRequestException) exception;
return Response
.status(400)
.entity(samplePackOrderBadRequestException)
.build();
}
return Response.status(400).build();
}
}
However this issue with this is that it deserializes super (Throwable), so you get every single inherited property added in the response which I do not want.
To combat this I tried adding Jackson annotations like so:
#JsonIgnoreProperties(value = "stackTrace")
This is not an optimal solution as there are several properties other than stackTrace that I will need to ignore.
So to summarise, how can I get Dropwizard to properly deserialize my CustomException class without all the additional clutter that I do not need?
I think the easier option is to transform exception to a Error bean and return it as shown below.
public class CustomBadRequestExceptionMapper implements ExceptionMapper<SamplePackOrderBadRequestException> {
#Override
public Response toResponse(final SamplePackOrderBadRequestException exception) {
if (exception instanceof SamplePackOrderBadRequestException) {
SamplePackOrderBadRequestException ex
= (SamplePackOrderBadRequestException) exception;
return Response
.status(400)
.entity(new ErrorBean(400,ex.getMessage,ex.getgetValidationFailures()))
.build();
}
return Response.status(400).build();
}
}
And ErrorBean.java
public static class ErrorBean{
private int code;
private String message;
private List<ValidationFailureDto> failures;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<ValidationFailureDto> getFailures() {
return failures;
}
public void setFailures(List<ValidationFailureDto> failures) {
this.failures = failures;
}
}
I created an exception :
public class PkDeleteException extends java.lang.Exception {
private static final long serialVersionUID = 1L;
public PkDeleteException(String msg) {
super(msg);
}
}
Now I threw it in the catch block of some code :
import com.ambre.pta.utils.PkDeleteException;
public class AdminRole {
#Autowired
private Environment env;
#Autowired
private RoleDAO roleDao;
public void del(#RequestParam String id) {
try {
roleDao.delete(id);
} catch (org.hibernate.exception.ConstraintViolationException e) {
Role role = roleDao.get(id);
String errMsg = env.getProperty("admin.list.profils.err.suppr");
errMsg = errMsg.replace("%s", role.getRole_lib());
throw new PkDeleteException(errMsg);
}
}
}
But I got an error Unhandled exception type PkDeleteException !
There are suggested solutions proposed by Eclipse but I do not want to follow them ! So why is there this error ?
In general or for most of the scenarios, you never create a custom exception by extending java.lang.Exception class directly, rather you need to extend java.lang.RuntimeException class (or it's subtypes, which is even more preferable).
As your current PkDeleteException is checked Exception, you need to declare that in your method signature using throws clause (option-2, not preferable) or the best practice is to convert it into unchecked Exception (option-1) like below:
Option(1) - Use unchecked Exception (Preferable):
public class PkDeleteException extends RuntimeExcetion {
private static final long serialVersionUID = 1L;
public PkDeleteException(String msg) {
super(msg);
}
}
Option(2): Change your method signature
from
public void del(#RequestParam String id)
to
public void del(#RequestParam String id) throws PkDeleteException
I suggest you to have a look at here
Your del method should throw PkDeleteException.
Your method should be like follow
public void del(#RequestParam String id) throws PkDeleteException {
try {
roleDao.delete(id);
} catch (org.hibernate.exception.ConstraintViolationException e) {
Role role = roleDao.get(id);
String errMsg = env.getProperty("admin.list.profils.err.suppr");
errMsg = errMsg.replace("%s", role.getRole_lib());
throw new PkDeleteException(errMsg);
}
}