Posts Tagged ‘error handling’
Throwing Custom Apex Exceptions
Throwing custom exceptions is something that most Java developers expect to be able to do when working with Apex code, and indeed it is possible. It isn’t very well documented, but I managed to stumble across this solution after some experimentation.
Usually I would expect an exception to be thrown using syntax at least similar to
throw new Exception('Don\'t be a silly user.');
However this syntax will give the follow error: Type cannot be constructed: Exception
Instead you have to create a new exception class with extends the Apex Exception class
public class myException extends Exception {}
And then throw that new exception
throw new myException('Don\'t be a silly user.');
That’s just the beginning of course, you could create some pretty fancypants error handling in your custom exceptions. The world is your oyster, now all your need is some Tabasco and lime.