+ Start a Discussion
kotoponuskotoponus 

Apex Workbook - Custom Exception by extending to builti-n Exception class

I am wondering if someone more experienced can tell me if I am following the instruction correctly. 

 

I am only working on the Apex Workbook as I am rather new to the apex coding:

 

http://www.salesforce.com/us/developer/docs/apex_workbook/Content/apex7_5.htm

 

I am trying out the point 1 which says:

 

>> 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.

>>

>> 1. In the Developer Console, create a class named MerchandiseException and add the following to it:

>>

>> public class MerchandiseException extends Exception {}

>>

 

I read the step 1 as following:

 

In the Developer Console, I go to File -> New -> Apex Class and type "MerchandiseException" as a name for my new Apex Class in the New Apex Class window.  Then press OK button.

 

However, when I pressed OK, it gave me this error:

 

INVALID_FIELD_FOR_INSERT_UPDATE

MerchandiseException: Exception class must extend another Exception class

 

And it does not create the class.

 

Can someone give me a pointer as to where I went wrong? Something tells me that I am missing something here.

 

I am in developer instance of the SF.  Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
kotoponuskotoponus

Hurrah!  I have just saved it!

 

Slept on it over night, and, with a fresh start, I went to Stepup -> App Setup -> Develop -> Apex Classes -> New button (in the list view of the classes) to add:

 

public class MerchandiseException extends Exception {}

 

And, presto, I have managed to save it.  They should have mentioned in the workbook that this needs to be done in the main UI not through the Developer Console as the workbook emphasises doing everything in the Console.  I presume this is an uninteded bug on the Developer Console.

All Answers

crop1645crop1645

wow - this is weird

 

I replicated your use case. It looks like the DeveloeprConsole is picking up on the string 'Exception' in 'MerchandiseException' as a special case.  Use a class name like this to workaround:  'MerchandiseXception'.

 

 

kotoponuskotoponus

Hi, Eric,

 

Thanks for the reply - so do you think the workbook did not anticipate this to happen?  I can see that Apex would pick "exception" as a reserved word and will flag up as an alert or something, which makes sense.  But I also expect that workbook would guide me through only the things I can do, hence, I started to wonder I am doing something wrong here.

kotoponuskotoponus

Hurrah!  I have just saved it!

 

Slept on it over night, and, with a fresh start, I went to Stepup -> App Setup -> Develop -> Apex Classes -> New button (in the list view of the classes) to add:

 

public class MerchandiseException extends Exception {}

 

And, presto, I have managed to save it.  They should have mentioned in the workbook that this needs to be done in the main UI not through the Developer Console as the workbook emphasises doing everything in the Console.  I presume this is an uninteded bug on the Developer Console.

This was selected as the best answer