0

I would like to know what are the constructors of Exception class in Salesforce.

I am aware of these two constructors, is there any other?

Exception()
Exception(String message)

We know that Salesforce is made over Java, so can we expect Salesforce to have the same set of "Constructors" as of java

Link here

Also, I would like to know if there is a way to know how the Exception class is written in Salesforce.

  • That last question is probably not on topic here. Even if it is on topic, try to stick to one question per question. – Adrian Larson Nov 7 '16 at 13:48
  • Hi @AdrianLarson, Thanks for your response. Kindly let me know whether I should raise a new question for the last topic.. Otherwise, if you know the answer, please let me know. – Kamruzzaman Nov 8 '16 at 7:13
  • As I said, the last question is almost certainly off topic on this forum, even if you create a separate question for it. – Adrian Larson Nov 8 '16 at 16:35
4

There are also Exception constructors that accept another Exception. Take a look at Creating Custom Exceptions, which shows constructors you can define on a class that simply extends Exception without any custom body.

To create your custom exception class, extend the built-in Exception class and make sure your class name ends with the word Exception. Append extends Exception after your class declaration as follows.

public class MyException extends Exception {}

Here are some ways you can create your exceptions objects, which you can then throw.

You can construct exceptions:

  • With no arguments:

    new MyException();
    
  • With a single String argument that specifies the error message:

    new MyException('This is bad');
    
  • With a single Exception argument that specifies the cause and that displays in any stack trace:

    new MyException(e);
    
  • With both a String error message and a chained exception cause that displays in any stack trace:

    new MyException('This is bad', e);
    

So there are at least two that you did not list (the last two). The document I linked does not claim to be comprehensive, but I believe these are indeed all of the public constructors.

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.