-1

I have no idea about how to use Eclipse to execute "java -ea -da:Derived AssertionCheck". Could you help me?

Thank you!

The classes are here:

class Base {
public void foo() {
assert true; // ASSERT_BASE
}
}

class Derived extends Base {
public void foo() {
assert false; // ASSERT_DERIVED
}
}
class AssertionCheck {
public static void main(String []args) {
try {
Base base = new Base();
base.foo();
}
catch(Exception e) {
base = new Derived();
base.foo();
}
}
}
Young
  • 31
  • 3
  • Welcome to Stack Overflow! It's not clear what you're asking here; why are you trying to use Eclipse to execute a Java command line? – Scott Barta May 10 '14 at 16:08

2 Answers2

1

https://stackoverflow.com/a/11415579/1791197 make it like Priyank Doshi shows and just insted of only "-ea" set all parameters "-ea -da:Derived AssertionCheck"

Community
  • 1
  • 1
KonradOliwer
  • 176
  • 6
0

In order to run, you need to move the Base base = new Base(); outside of the try statement like this:

class AssertionCheck {
   public static void main(String []args) {
      Base base = new Base();
      try {
         base.foo();
      }
      catch(Exception e) {
         base = new Derived();
         base.foo();
      }
   }
}

I'm not sure what your trying to test....test enabling assertions on command line, or test capturing the Assertion being thrown by the Base class.

If the Base class had "assert.false", but not on the Derived class then the catch statement above would not be successful. Assertions throw Errors and not Exceptions, so you would need to add

catch (AssertionError er) {
   base = new Derived();
   base.foo():
}

Normally AssertionError are not used or implemented in production code, because it usually means there is a problem within the code than is not be handled correctly.

Brian
  • 450
  • 5
  • 11