Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I got the following error when i try to create a test class which it seems to be legitimate. But it complains error. I do not know what is issue in this class.

Save error: testCustomException: Exception class must extend another Exception class testCustomException.cls /DMSand_25Apr2014/src/classes line 2 Force.com save problem

Test class:

    @isTest
private class testCustomException {

    static testMethod void myUnitTest() {
        try
        {
            throw new CustomException('this ','is my ','message');
        }
        catch(Exception e)
        {
            system.assertEquals('Script-thrown exception', e.getMessage());
        }
    }
}

Custom Exception class:

public with sharing class CustomException extends Exception {
    public String ErrorCode {get;set;}
    public String ErrorType {get;set;}
    public String ErrorDescription {get;set;}

    public CustomException(String ErrCode, String ErrType, String ErrDesc) {
        this.ErrorCode = ErrCode;
        this.ErrorType = ErrType;
        this.ErrorDescription = ErrDesc;
    }
}
share|improve this question
1  
Ensure you do not have a local class that you have called Exception; to check that extend System.Exception. –  Keith C Apr 25 at 14:57
    
Thanks for your response, Keith. As per you advice on other day, i have been naming the classes and other components so that it will not collide with the standard one. Here, i do not have a class with name "Exception". –  Baskaran S Apr 25 at 14:59

3 Answers 3

up vote 4 down vote accepted

The problem is that your test class name ends with Exception. Salesforce does not allow classes with names that end with Exception unless they extend the Exception class.

I'd suggest renaming your test class to CustomExceptionTest to get around this.

share|improve this answer
    
Thanks Alex. I am going to change the class name. –  Baskaran S Apr 25 at 16:15

After saving a class that extends an Exception you will get an error in the developer console when the class name does not end with Exception.

Try creating the Exception extends class directly from setup>Develop>Apex classes.

share|improve this answer

A custom exception must tie into the Exception plumbing and therefore must extend a standard exception. To do what you're trying to do, just declare your exception in your class:

public class CustomException extends Exception {}
share|improve this answer
    
Thanks greenstork. Actually, i created the custom exception by extending the Super class Exception class first and created this test class and it throws error on saving it in Eclipse. –  Baskaran S Apr 25 at 14:55
    
Fun thing is right now, at least using the developer console, if you try and create a class "TestException" it tells you the class must extend exception (kind of hard to do when you can't yet edit the body), and of course, if you create the class first, extending exception generates an error stating class name must end in exception, wtf –  Ralph Jun 10 at 4:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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