Home > Mobile >  Unreachble catch block java exercise
Unreachble catch block java exercise

Time:08-18

I'm doing some java exercise for an exam and I have a problem in some of them, like this: This is the code:

public class A1 {

public static void main(String [] argv)
        throws Exception {
    try {
        m();
        System.out.print(1);
    }
    catch( Exception u ) {
        System.out.print(2);
    }
    finally {
        System.out.print(3);
        throw( new MyExc2() );
    }
}
static void m()
        throws Exception {
    try {
        System.out.print(4);
    }
    catch( MyExc2 t ) {
        System.out.print(5);
    }
    catch( MyExc1 h ) {
        throw( new MyExc1() );
    }
    catch( Exception b ) {
    }
    finally {
        System.out.print(6);
    }
  }
}

and the Exception hierarchy is:

class MyExc1 extends Exception { }
class MyExc2 extends Exception { }
class MyExc3 extends MyExc2 { }

The compiler give me this error: "Unreachable catch block for MyExc2. This exception is never thrown from the try statement body"? Why is important the fact that is unreachable, can't the compiler just ignore it? (sorry for my poor english, hope you can understand)

CodePudding user response:

This is a design choice on the language level.

Unreachable code means: dead code. Lines of code that sit there and do nothing. From a technical point of view, they do not matter, the compiler will simply not compile them.

But remember that source code is "mainly" written for human readers: and human readers have to spend "brain CPU cycles" to read and understand code. So why force humans to invest that energy for dead code?

And worse: it could mislead human readers. People might just glance over some code and draw conclusions on that dead code. For example, they might start looking into the code in the try block, trying to identify where that exception might come from. Or even worse: they do not think about that, but start copying that code around, including the useless try/catch.

And back then, when Java was designed, people put an emphasis on a "clean" language, that results in code that is easy to read and understand, and that makes it hard to do things the wrong way.

It is a trade off, and the language designers decided to do it like this.

Another angle: as a programmer, you always strive to write the smallest amount of code that implements a feature, you don't want your code base to turn into a junkyard. And sometimes the compiler can help you achieving that goal.

CodePudding user response:

The code in catch block for MyExc2 is unreachable, because the println method called at System.out.print(4) does not declare possibility of throwing the MyExc2 exception.

CodePudding user response:

The answer you asked is more to know why is unreachable, but i am going to leave my pov:

There are some exceptions that are never treated in the body of the method, instead, you throw it just a side of the method like this:

    public Object nMethod(nParamType nParamName) throws myExc2 {}

Try to solve your excersice in that way, and i sorry as well for the poor english

  • Related