function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jim LernerJim Lerner 

Unable to extend (sub-class) Exception

I'm trying to extend the System.Exception class to add some logging and email functionality.  My interest is in having a constructor like this:

class APException extends Exception {
  public APException(String message) {
    super(message);
    logException();
    mailNotify();
  }
}

This code gives me a compile-time error: System exception constructor already defined: <Constructor>(String, Exception).  

I've even tried adding a dummy argument so that the method signature doesn't match any existing Exception constructor.  That only leads to another error: Object has no superclass for super invocation.

What is the right way to do this?  Or can it not be done?
Pankaj_GanwaniPankaj_Ganwani
Hi Jim,

You can simply use below:

public with sharing class APException extends Exception { }

And wherever you want to show the message use throw statement like this:

if(Account.Id == null)
    throw new APException (Error');

catch(APException ex)
{
      
}
 
Jim LernerJim Lerner
I didn't know about "with sharing", so thanks for that!  However, I'm still getting a compile-time error on the constructor, whose signature is:
public APException(String msg, Exception originalException)

System exception constructor already defined: <Constructor>(String, Exception)
Jim LernerJim Lerner
Do my problems somehow stem from the override keyword (or lack thereof)?
Pankaj_GanwaniPankaj_Ganwani
Hi Jim,

You can ignore the with sharing keyword though. Can you please refer this link:
http://www.salesforce.com/us/developer/docs/apex_workbook/Content/apex7_5.htm
Jim LernerJim Lerner
I get that I "can" inherit the constructor and don't need to create my own.  The problem is that I really want to override the constructor, so that the APException gets logged and emailed when it is thrown.  Without overriding it, I would need to place awareness of the extra functions into every caller or catcher.  That's why my proposed constructor first calls super(), then does the other stuff.
Douglas HauckDouglas Hauck
Old thread, I know, but I implemented it thusly:
public class MyException extends Exception
{
    integer exLineNumber = -1;
    
    public MyException (string message, integer lineNumber)
    {
        this.setMessage(message);
        exLineNumber = lineNumber;

        // Do some other stuff...
    }
}
You would have to do the same for each of the base Exception constructors, or at least those you plan on using.  To my knowledge, there are 4 constructors for the Exception superclass:
  • Exception()
  • Exception(string message)
  • Exception(Exception initCause)
  • Exception(string message, Exception initCause)
Yes, this method means you still have to use a dummy parameter to avoid matching the base constructor signatures.  I don't like it either, but that's something for the Ideas section.  And there must be something you can add that would be useful to you, like the line number or some such.  Hope that helps a little.

Best,
Doug