50

Code:-

try {
    Assert.assertEquals("1", "2");
} catch (Exception e) {
    System.out.println("I am in error block");
}

If the assert statements fails, I would like to capture the error in the catch block. I am trying with the above code and its not happening.

Will the assertion error be caught by in a catch block for java exception?

J Richard Snape
  • 18,946
  • 5
  • 47
  • 73
Galet
  • 4,039
  • 12
  • 62
  • 119
  • 2
    Why would you want to do that anyways? This seems like a gross misuse of `Assert` which is meant for Unit Testing – Dragondraikk Apr 16 '15 at 10:25

4 Answers4

72

You have almost answered your own question. Your catch block will not catch the AssertionError that the Assert throws if it fails, because it is an Error (or, more specifically, it extends java.lang.Error). See the docs for more information on this. Your catch block only catches Throwable objects that extend java.lang.Exception

If you really want to catch it - you need to use

catch (AssertionError e) {
...

However, as others have mentioned, this is a very unusual way to use assertions - they should usually pass and if they fail it is very unusual for you to want to carry on program execution. That's why the failure throws an Error rather than an Exception. You can read more about (not) catching Error in this question.

Are you sure you don't just want a test - if ( variableName == "1")?

NB if you are testing unit-test helper code, like a matcher, it might make sense to catch the AssertionError.

Community
  • 1
  • 1
J Richard Snape
  • 18,946
  • 5
  • 47
  • 73
  • 2
    When one wants to see if a unit-test helper-class is doing the right thing (by unit-testing it ;-) ) one would expect to be able to catch such a AssertionError. That would be a valid situation to catch it, wouldn't it? – gkephorus Jan 18 '16 at 08:05
  • @gkephorus yes, I suppose it could be. Feel free to edit that in as an "N.B." if you like and I'll approve it :) – J Richard Snape Jan 18 '16 at 09:49
  • @JRichardSnape - I want to compare two java objects (obtained from json objects) which have only string variables with getters & setters i.e. POJO. I only want to compare some (not all) of the variables to determine a match. For one of the variables, only partial value should be compared. Is it a good idea to put all of my asserts into a try catch(AssertionError) block ? That way, I can return false in the catch part to indicate which field mismatched, with a useful error message string. This way, I won't have to write several if else. Sounds ok ? – MasterJoe Mar 09 '18 at 05:46
  • 1
    With respect to: _"NB if you are testing unit-test helper code, like a matcher, it might make sense to catch the AssertionError"_ - I would recommend using `org.junit.rules.ExpectedException.handleAssertionErrors()` – josle Feb 07 '19 at 16:36
  • `catch (AssertionError e) { }` is not invoked in my case. Thou shall not catch `AssertionError` is the mantra here. – eigenfield Jan 02 '20 at 10:16
7

If you want to catch both Exception and Error instances use:

...
catch (Throwable t)
{
...
}

Since both Exception and Error extend Throwable.

wilmol
  • 641
  • 6
  • 14
4

Well, I believe you are using JUnit for writing your tests. In that case, you should not catch your Assert.assertEquals() because they should pass for normal test execution. If it throws any exception, it means that your code is not performing as it should.

Aakash
  • 1,875
  • 13
  • 22
  • yes my assert code throws an exception. But i want to capture all errors such assertions or any other error by Exception class. How can i do it ? – Galet Apr 16 '15 at 10:28
  • 2
    You can't. `AssertionError` inherits from `Error` which means `try...catch` won't be able to handle them. See here https://docs.oracle.com/javase/7/docs/api/java/lang/AssertionError.html. If you anyways need to catch them, see SpaceCowboy's answer. – Aakash Apr 16 '15 at 10:33
  • @Aakash that's incorrect - this works fine: `try { throw new AssertionError(); } catch( AssertionError e){ System.out.println("ok");}` – danyamachine Aug 21 '18 at 19:37
  • @danyamachine as I said, we **should** not catch `AssertionError` or any `Error` for that matter. Errors are there to tell developer that something wrong has happened and program should not continue. It's a late reply, but better late than never :) – Aakash Oct 20 '19 at 16:18
0

If you want to catch the errors in that way you need something like the following:

if (num == 1 || num == 2) {
    throw new Exception();
}

You could create your own exception class and pass in the message you want.

SpaceCowboy
  • 507
  • 6
  • 16