You need to sign in to do that
Don't have an account?
Jim 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?
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?
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)
{
}
public APException(String msg, Exception originalException)
System exception constructor already defined: <Constructor>(String, Exception)
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
- 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