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
sdudasduda 

Throwing Exceptions?

How do I throw one of the system defined exception types?

it says: throw <ExceptionObject>

I've tried:

throw new StringException();
throw new System.StringException();
throw StringException;

It returns the error message:

Exception type cannot be constructed

Thanks
Ron HessRon Hess
i believe those are reserved for the system.

you should be able to create your own exception (possibaly extending a system exception) and then create and throw it.

let us know how this works, i've not tried it (yet)
sdudasduda
So I added this class def. inside the class I was throwing the exception from:

public class NotificationRecordException extends Exception {}


ex:

public class NotificationRecord {
 
    public class NotificationRecordException extends Exception {}

    public static void testException() {
       throw new NotificationRecordException('There was an exception');
    }
}


...... And then from another class's method:


try {
    NotificationRecord.testException();
} catch ( NotificationRecord.NotificationRecordException e ) {
    System.debug(e);
}