简体   繁体   中英

How can I add condition in custom exception class in java

I have a class where I call in several places a custom class to handle exceptions.

public class PackageFailedException extends Exception {
    public PackageFailedException(String msg) {
        super(msg)
    }
}

I need to add a condition if a variable is true then ignore the exception. Is there a way to do it in one place? for example in my custom class ?

Thanks in advance!

You can simply add the flag to your Exception.

public class PackageFailedException extends Exception {

    private final boolean minorProblem;

    public PackageFailedException(String msg, boolean minorProblem) {
        super(msg);
        this.minorProblem = minorProblem;
    }

    public boolean isFlag() {
       return this.flag;
    }
}

Then you can simply call isMinorProblem() and decide if to ignore it. The assumption here is that you can pass it when it is being thrown.

If the flag indicates a substantially different error situation, however, you might want to consider a different Exception class altogether instead , maybe extending PackageFailedException if it is a more specialised case of it.

 public class MinorPackageFailedException extends PackageFailedException {

     public MinorPackageFailedException(String msg) {
       super(msg);
     }
 }

Then in your code:

try {
  try {
    doThePackageThing();
  } catch (MinorPackageFailedException ex) {
    //todo: you might want to log it somewhere, but we can continue 
  }

  continueWithTheRestOfTheStuff();

} catch (PackageFailedException ex) {
  //todo: this is more serious, we skip the continueWithTheRestOfTheStuff();
}

You conditionally create the exception, so it only gets thrown if and when appropriate.

Either that and/or you handle the exception differently based on conditions when it is caught.

What you don't do is have the exception decide whether it should exist or not, that way lies madness.

You can inherit your PackageFailedException, to create a logic like this:

public class IgnorablePackageFailedException extends PackageFailedException {

    public IgnorablePackageFailedException(final String msg) {
        super(msg);
    }
}

Then, in your code you can throw PackageFailedException or IgnorablePackageFailedException. For Example:

public static void method1() throws PackageFailedException {
    throw new PackageFailedException("This exception must be handled");
}

public static void method2() throws PackageFailedException {
    throw new IgnorablePackageFailedException("This exception can be ignored");
}

So, you can handle the exceptions like this:

public static void main(final String[] args) {
    try {
        method1();
    } catch (final PackageFailedException exception) {
        System.err.println(exception.getMessage());
    }

    try {
        method2();
    } catch (final IgnorablePackageFailedException exception) {
        System.out.println(exception.getMessage()); // Ignorable
    } catch (final PackageFailedException exception) {
        System.err.println(exception.getMessage());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM