tawheed
tawheed

Reputation: 5821

Executing code after exception is thrown

So I have a bit of situation here with my design and was wondering whether I could get some feedback.

public class Class1 {
    public void eatFish(){}
}

public class Class2 {
    public void eatBacon(){
        // some nasty code here to cause an exception
    }
}

public class Class3 {
    public void eatFruit(){}
}

public InvokeAllClasses() {
    public static void main(String[] args) {
        Class1 c1 = new Class1();
        Class2 c2 = new Class2();
        Class3 c3 = new Class3();

        c1.eatFish();
        c2.eatBacon();
        c3.eatFruit();
    }
}

See the problem here in InvokeAllClasses is that, because c2.eatBacon(); blows up, c3.eatFish() would not be executed. Is there a way to still execute c3 although c2 blew up?

Update

After thinking more about, I guess I could wrap each call in a try...catch block but that is just messy.

Upvotes: 0

Views: 1144

Answers (6)

plalx
plalx

Reputation: 43718

Unless you are sure that you will never have to handle any exceptions thrown by those methods, it might be better to avoid swallowing all of them at the source.

It's been a while since I wrote Java code and I could not try and compile it, but the idea is to create an object which has the responsability to execute tasks and swallow any exceptions.

It may look like:

public class SilentExecutor {
    List<Runnable> tasks;

    public SilentExecutor(List<Runnable) tasks) {
        this.tasks = tasks == null? new List<Runnable>() : tasks;
    }

    public void execute() {
        for (Runnable task : this.tasks) silentlyExecute(task);
    }

    private void silentlyExecute(Runnable task) {
        try { task.run(); }
        catch (Exception e) {}
    }
}

Then your code could be something like:

new SilentExecutor(Arrays.asList(
    () -> { c1.eatFish(); },
    () -> { c2.eatBacon(); },
    () - > { c3.eatFruit(); }
)).execute();

Upvotes: 1

JustinKSU
JustinKSU

Reputation: 4989

There are two approaches you could take. The first option is to ignore the exceptions completely.

try {
  c1.eatFish();
} catch(Exception e) {//Ignore}
try {
  c2.eatBacon();
} catch(Exception e) {//Ignore}
try {
  c3.eatFruit();
} catch(Exception e) {//Ignore}

If you want the exception to be thrown in the end, you can put the result into a variable and then throw it at the end or use the finally clause.

try {
  c1.eatFish();
finally {
  try {
     c2.eatBacon();
  } finally {
     c3.eatFruit();
  }
}

If you are looking for something more readable, you could wrap the method calls.

   public static void main(String[] args) {
        Class1 c1 = new Class1();
        Class2 c2 = new Class2();
        Class3 c3 = new Class3();

        callEatFishIgnoringException(c1);
        callEatBaconIgnoringException(c2);
        callEatFruitIgnoringException(c3);
    }

    private static void callEatFishIgnoringException(Class1 c1) {
      try {c1.eatFish()} catch (Exception e) {//Ignore}
    } 

    private static void callEatBaconIgnoringException(Class2 c2) {
      try {c2.eatBacon()} catch (Exception e) {//Ignore}
    } 

    private static void callEatFruitIgnoringException(Class3 c3) {
      try {c3.eatFruit()} catch (Exception e) {//Ignore}
    } 

Upvotes: 0

chntgomez
chntgomez

Reputation: 2119

Make your method to throw an exception.

public InvokeAllClasses() throws Exception {
public static void main(String[] args) {
    Class1 c1 = new Class1();
    Class2 c2 = new Class2();
    Class3 c3 = new Class3();

    try{
        c1.eatFish();
    }catch(Exception e){System.out.println("Oh noes! There was something wrong!!!")}
    finally{
        c2.eatBacon();
        c3.eatFruit();  
    }

}

As you can see. The "finally" statement will force your code to perform no matter if the statement inside the try fails or throws an exception.

Upvotes: 0

Anoop Dixith
Anoop Dixith

Reputation: 643

Why not just catch the exception and move on? I honestly don't think it will be messy.

Upvotes: 0

Put the try...catch in the method defintion:

public void eatBacon(){
    try{
        // some nasty code here to cause an exception
    } catch(Exception e){
        //do something
    }
}

This won't look as bad as putting it when you call the method. If you know where exactly in the code the exception could be happening, then only surround those statements.

Upvotes: 5

Evan Frisch
Evan Frisch

Reputation: 1374

You could handle the exceptions within the methods themselves so they aren't thrown back up to the calling method, but other than try/catch/finally blocks, there isn't a good practice way to ignore exceptions.

Upvotes: 1

Related Questions