Find your content:

Search form

You are here

Custom Exception with a constructor that takes parameters

 
Share

I'd like to extend Exception to create a custom exception class that takes several arguments in addition to the standard string message.

I've found examples showing basic inheritance (An Introduction to Exception Handling) and adding custom constructors (Extended Class Example).

public virtual class MyException extends Exception {
    public Double d;

    // Exception class constructor     
    MyException(string message, Double d) {
        // How can I pass 'message' to the base constructor?
        this.d = d;
    }
}

When using a custom exception similar to the example above calls to getMessage() return 'Script-thrown exception', which makes sense as message hasn't been utilized yet.

How can I extend Exception and include a custom constructor such that Exception.getMessage() returns a string specified in the constructor?

Or more generally, how do I pass arguments from my custom constructor to the base constructor in Apex?


Attribution to: Daniel Ballinger

Possible Suggestion/Solution #1

I found one possible solution in the post Custom Exception message by Corey_B.

In the custom constructor explicitly call setMessage. E.g.

public virtual class MyException extends Exception {
    public Double d;

    // Exception class constructor     
    MyException(string message, Double d) {
        // Pass 'message' to the base class
        this.setMessage(message);

        this.d = d;
    }
}

This does solve my explicit issue, but doesn't really address how to handle base constructors with inheritance.


Update for "how do I pass arguments from my custom constructor to the base constructor in Apex?"

I tried using super(message); as the first line of the constructor but it resulted in the error:

Save error: Object has no superclass for super invocation

So I'm assuming that Exception isn't virtual.

"Only classes that are extending from virtual or abstract classes can use super." [Super keyword]


Attribution to: Daniel Ballinger

Possible Suggestion/Solution #2

This.setMessage() seems like the only plausible alternative. If you're just bothered about the message you could try this. http://th3silverlining.com/2009/06/11/throwing-custom-apex-exceptions/


Attribution to: techtrekker

Possible Suggestion/Solution #3

You can use this(message) in your constructor to call the base class constructor:

public virtual class MyException extends Exception
{
    public Decimal d;

    public MyException(String message, Decimal d)
    {
        this(message);
        this.d = d;
    }

    @isTest
    static void test()
    {
        try
        {
            throw new MyException('this is my message',3.1415);
        }
        catch(Exception e)
        {
            system.assertEquals('this is my message', e.getMessage());
            MyException me = (MyException) e;
            system.assertEquals('this is my message', me.getMessage());
            system.assertEquals(3.1415, me.d);
        }
    }
}

However, I would have expected the System Exception class to behave like a virtual Apex class (and therefore be able to use super(message) in this situation).


Attribution to: Stephen Willcock

Possible Suggestion/Solution #4

I think following can help you. It has complete example of SFDC APEX standard and custom exceptions

http://share-salesforce.blogspot.in/2013/05/salesforce-apex-exception-handling-and_29.html


Attribution to: user2923
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1811

My Block Status

My Block Content