Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching Multiple Exceptions - calling methods not present in Exception on the caught exception

public class try
{
    public static void main(String[] args)
    {
        try
        {
            if(true)
                throw new A();
            if(true)
                throw new B();
        }
        catch( A | B e)
        {
            e.doit();
        }
    }
}

class A extends Exception
{
    public void doit() {}
}

class B extends Exception
{
    public void doit() {}
}

This doesn't compile

18: error: cannot find symbol
        e.doit();
         ^
symbol:   method doit()
location: variable e of type Exception

The variable e seems to end up as type Exception rather than the actual type - this seems logical because at compile type the compiler doesn't know what kind is going to be thrown. However, is there a way to make this work without making A & B both derive from some common base class or implement a common interface?

like image 805
user93353 Avatar asked Oct 17 '14 09:10

user93353


People also ask

How do you catch multiple exceptions in one catch?

Java allows you to catch multiple type exceptions in a single catch block. It was introduced in Java 7 and helps to optimize code. You can use vertical bar (|) to separate multiple exceptions in catch block.

Can you catch multiple exceptions?

When catching multiple exceptions in a single catch block, the rule is generalized to specialized. This means that if there is a hierarchy of exceptions in the catch block, we can catch the base exception only instead of catching multiple specialized exceptions.

Is there a way to catch multiple exceptions at once and without code duplication?

In C#, You can use more than one catch block with the try block. Generally, multiple catch block is used to handle different types of exceptions means each catch block is used to handle different type of exception.

How do you handle multiple exceptions with a single except clause in java?

The * symbol indicates that multiple exceptions can be handled by each except* clause. try: raise ExceptionGroup('Example ExceptionGroup', ( TypeError('Example TypeError'), ValueError('Example ValueError'), KeyError('Example KeyError'), AttributeError('Example AttributeError') )) except* TypeError: ...


2 Answers

Well no, because Java doesn't support duck typing.

Doing an instanceof and casting e to either A or B is obviously going to work, but what you probably want to do in this case is doing it in the traditional way of writing two catch blocks.

I mean, it makes sense, right? Multi-catch is appropriate in the case where you want to treat different kinds of exceptions equally. In this case, the behaviour can be drastically different (even if the methods are named the same).

like image 90
Jan Van den bosch Avatar answered Sep 22 '22 11:09

Jan Van den bosch


You should just create a super class that both A and B extend, give that class an doIt() method, and then implement that method for both A and B, like this:

  class A extends C {
    public void doit() {
    }
  }

  class B extends C {
    public void doit() {
    }
  }

  abstract class C extends Exception {
    public abstract void doit();
  }

You can then catch C, like this:

try
{
  if(true)
    throw new A();
  if(true)
    throw new B();
}
catch( C e)
{
  e.doit();
}
like image 25
PeterK Avatar answered Sep 22 '22 11:09

PeterK