Home > front end >  How to write custom excetion for Mutilple exceptions in java?
How to write custom excetion for Mutilple exceptions in java?

Time:03-09

I am using smtp api which throws MessageException and IOException

But in our application, we need to have wrapper exception for both.

Is it possible to write wrapper exception for this? like custom exception?

CodePudding user response:

Sure. Exceptions just are, and can wrap anything; you don't need to write them as specifically wrapping only IOException or MessageExceptions.

public class MyCustomException extends Exception {
  public MyCustomException(String msg) {
    super(msg);
  }

  public MyCustomException(String msg, Throwable cause) {
    super(msg, cause);
  }
}

The above is what all custom exceptions look like (where relevant they might have a few more fields that register specific info for a specific failure, e.g. SQLException has methods to ask for the DB 'error code'), but they all at least have the above.

Then, to wrap:

public void myMethod() throws MyException {
  try {
    stuffThatThrowsIOEx();
    stuffThatThrowsMessageEx();
  } catch (MessageException | IOException e) {
    throw new MyException("Cannot send foo", e);
  }
}

NB: The string you pass to your MyException should be short, should not use either caps or exclamation points, or for that matter any other punctuation at the end of it. In addition, include actual relevant content there too: For example, the user you tried to send a message for (the point is, whatever you include there as a string constant needs to be simple, short, and not end in punctuation).

CodePudding user response:

Consider to create a root Exception container as

public class GeneralExceptionContainer extends RuntimeException{
    private Integer exceptionCode;
    private String message;

    public GeneralExceptionContainer(String argMessage, Integer exceptionCode) {
        super(argMessage);
        this.exceptionCode = exceptionCode;
        this.message = argMessage;
    }

    public GeneralExceptionContainer(Throwable cause, Integer exceptionCode, String argMessage) {
        super(argMessage, cause);
        this.exceptionCode = exceptionCode;
        this.message = argMessage;
    }
}

With some enumeration or serialization requirement you can add exceptionCode as well

public enum ExceptionCode {
    SECTION_LOCKED(-0),
    MAPPING_EXCEPTION(-110)

    private final int value;

    public int getValue() {
        return this.value;
    }

    ExceptionCode(int value) {
        this.value = value;
    }

    public static ExceptionCode findByName(String name) {
        for (ExceptionCode v : values()) {
            if (v.name().equals(name)) {
                return v;
            }
        }
        return null;
    }
}

Then extend your customException from root GeneralException Containner

public class CustomException extends GeneralExceptionContainer {
    public MappingException(ExceptionCode exceptionCode) {
        super(exceptionCode.name(), exceptionCode.getValue());
    }
}
  • Related