Exception class must extend another Exception class. Why?

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;
    }
}

Answer

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.

Attribution
Source : Link , Question Author : Bforce , Answer Author : Alex Tennant

Leave a Comment