4

I'm writing a testMethod for a custom Exception class.

As part of the testMethod I want to deliberately throw another exception to become the inner cause of my custom exception.

E.g.

public class CustomException extends Exception {

    public Id opportunityId {
        get;
        private set;
    }

    public CustomException(Id exceptionOpportuntiyId, string message, Exception innerException) {
        this.opportunityId = exceptionOpportuntiyId;
        this.setMessage(message);
        this.initCause(innerException);
    }
}

And the corresponding testMethod:

private static testMethod void customExceptionTest() {
    Id opportuntiyId = '006000000000000';
    try {
        Savepoint sp = Database.setSavepoint();
        try {

            integer foo = null;
            // Intentional NullPointerException
            foo = foo + 1;
            System.assert(false, 'Null Exception Expected');
        } catch (NullPointerException ex) {
            Database.rollback(sp);
            throw new CustomException(opportuntiyId, 'customExceptionTest', ex);
        }
        System.assert(false, 'CustomException Expected');
    } catch (CustomException saveEx) {
        System.assertEquals(opportuntiyId, saveEx.opportunityId);
        System.assertEquals('customExceptionTest', saveEx.getMessage());
    }
}

Oddly, the assertion in the inner try block is causing the test method to fail. The line foo = foo + 1 executed without throwing an exception!

This seemed odd, so I tried simplifying.


The following produces the expected NullPointerException when run as anonymous apex:

integer foo = null;
foo = foo + 1;

12:38:48:016 FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object


The wrapping the same code in a try/catch block:

try {
    integer foo = null;
    foo = foo + 1;
    System.debug('foo: ' + foo);
} catch (NullPointerException ex) {
  System.debug('Exception');
}

12:42:28:013 USER_DEBUG [6]|DEBUG|foo: null

The expected NullPointerException didn't occur. Instead foo is null!


I've raised with support (Case #08416231), but is anyone else aware of this or can confirm it is occurring for them as well?

Ideally I'd like a known issue reference or existing case number.

  • 1
    I get the same, most odd. However do this and it works... integer foo = null; try { foo = foo + 1; System.debug('foo: ' + foo); } catch (NullPointerException ex) { System.debug('Exception'); } – Andrew Fawcett Nov 19 '12 at 23:59
  • @AndrewFawcett, Thanks for confirming. I'll use your suggestion to get the test case passing. Still, I find this kind of bug concerning. :) – Daniel Ballinger Nov 20 '12 at 0:06
  • re: concerning, agree. Do let us know what comes of your support case if you can. – Andrew Fawcett Nov 20 '12 at 0:08
  • Will do, might take some time. I'm still waiting for a resolution to the case I raised 15 days ago for the other NullPointerException issue I had. – Daniel Ballinger Nov 20 '12 at 0:20
  • @AndrewFawcett Support responded today that they have confirmed the issue and will be working on a fix. No ETA or known issue reference number yet. – Daniel Ballinger Dec 3 '12 at 3:01
5

I get the same and also agree it looks like an Apex runtime bug. Perhaps relating to scope, as moving the variable declaration and init out of the try scope works...

Integer foo = null;
try {
    foo = foo + 1;
    System.debug('foo: ' + foo);
} catch (NullPointerException ex) {
  System.debug('Exception');
}
  • That works, thanks. In my test case I went with integer foo = (integer)null + 1;. Moving the foo deceleration didn't work there (I suspect due to the two levels of try/catch). – Daniel Ballinger Nov 20 '12 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.