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
Nisha WarrierNisha Warrier 

Custom Exception

Hi,
As a newbie, I was trying creating a custom Exception class using developer console.I tried  new>Apexclass.Gave the name MyException.
But it couldn't create the class as it says the class should extend Exception.But I can't write the code without creating the class first in developer console.I know that we do it directly in Setup>Develop>Apex ClassesUser-added image .But is there any reason why this is happening?
Mahesh DMahesh D
Hi Nisha,

In general when you are writing your own Exception Class it should extends the Exception generic class.

Creating Custom Exceptions
Since you can’t throw built-in Apex exceptions but can only catch them, you can create custom exceptions to throw in your methods. That way, you can also specify detailed error messages and have more custom error handling in your catch blocks.

To create your custom exception class, extend the built-in Exception class and make sure your class name ends with the word Exception. Append extends Exception after your class declaration as follows.
 
public class ***MyException*** extends Exception {}

Here are some ways you can create your exceptions objects, which you can then throw.
You can construct exceptions:
With no arguments:
 
new MyException();

With a single String argument that specifies the error message:
 
new MyException('This is bad');

With a single Exception argument that specifies the cause and that displays in any stack trace:
 
new MyException(e);

With both a String error message and a chained exception cause that displays in any stack trace:
new MyException('This is bad', e);
Now that you’ve seen how to create an exception class and how to construct your exception objects, let’s create and run an example that demonstrates the usefulness of custom exceptions.
In the Developer Console, create a class named MerchandiseException and add the following to it:
 
public class MerchandiseException extends Exception {}

You’ll use this exception class in the second class that you’ll create. Note that the curly braces at the end enclose the body of your exception class, which we left empty because we get some free code—our class inherits all the constructors and common exception methods, such as getMessage, from the built-in Exception class.

Also look into the below useful links:

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex7_5.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_exception_methods.htm

http://salesforce.stackexchange.com/questions/33692/exception-class-must-extend-another-exception-class-why

http://th3silverlining.com/2009/06/11/throwing-custom-apex-exceptions/


Please do let me know if it helps you.

Regards,
Mahesh
Nisha WarrierNisha Warrier
Thanks Mahesh,
I know that we should extend Exception class .But developer console is not allowing to create the class itself.Once I create the class, I can write the code "extends Exception".But it is not allowing me to create the class itself thru console.