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
zoranszorans 

Save error: unexpected token: public

Hi all,

 

I was learning new things from an apex_workbook and on exception chapter when I tried to create custom exception I was using two classes in one file(book example)

 

Because I am new in all this, I couldn't determine how to handle an error in this (Save error: unexpected token: public) code

 

Error occures in 3. row: it underlines reserved word 'public' ...

 

public class MerchandiseException extends Exception{}

public class MerchandiseUtility{
	
	public static void mainProcessing(){
		try{
			insertMerchandise();
		}catch(MerchandiseException me) {
			System.debug('Message: ' + me.getMessage());
			System.debug('Cause: ' + me.getCause());
			System.debug('Line number: ' + me.getLinenumber());
			System.debug('Stack trace : ' = me.getStackTraceString());
		}
	}
	
	public static void InsertMerchandise(){
		try{
			Merchandise__c m = new Merchandise__c();
			insert m;
		}catch(DMLException e){
			throw new MerchandiseException('Merchandise item could not be inserted ' + e)
		}
	}
	
}

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
mauro_offermannmauro_offermann

You must put MerchandiseException class as an inner class of MerchandiseUtility:

 

public class MerchandiseUtility{

public class MerchandiseException extends Exception{}
	
	public static void mainProcessing(){
		try{
			insertMerchandise();
		}catch(MerchandiseException me) {
			System.debug('Message: ' + me.getMessage());
			System.debug('Cause: ' + me.getCause());
			System.debug('Line number: ' + me.getLinenumber());
			System.debug('Stack trace : ' = me.getStackTraceString());
		}
	}
	
	public static void InsertMerchandise(){
		try{
			Merchandise__c m = new Merchandise__c();
			insert m;
		}catch(DMLException e){
			throw new MerchandiseException('Merchandise item could not be inserted ' + e);
		}
	}
	
}

 

Also notice that you miss a ; at the end of the following line:

throw new MerchandiseException('Merchandise item could not be inserted ' + e)

All Answers

mauro_offermannmauro_offermann

You must put MerchandiseException class as an inner class of MerchandiseUtility:

 

public class MerchandiseUtility{

public class MerchandiseException extends Exception{}
	
	public static void mainProcessing(){
		try{
			insertMerchandise();
		}catch(MerchandiseException me) {
			System.debug('Message: ' + me.getMessage());
			System.debug('Cause: ' + me.getCause());
			System.debug('Line number: ' + me.getLinenumber());
			System.debug('Stack trace : ' = me.getStackTraceString());
		}
	}
	
	public static void InsertMerchandise(){
		try{
			Merchandise__c m = new Merchandise__c();
			insert m;
		}catch(DMLException e){
			throw new MerchandiseException('Merchandise item could not be inserted ' + e);
		}
	}
	
}

 

Also notice that you miss a ; at the end of the following line:

throw new MerchandiseException('Merchandise item could not be inserted ' + e)

This was selected as the best answer
zoranszorans

Hey Maur0,

 

thank you very much for help it solves my problem.

 

I was wondering if you or somebody else could tell me couple other thigs about classes.

 

As I understooded classes can be inherited/extended with other classes that is clear and in this example that class must be in inside onde that is used.

 

But let's say I have two classes inside one file and they are not extended by any other;

public class A{//some code ...}

public class B{//some code that is using methods from class A ...}

 

Is in this situation also the case that class 'A' has to be inside class 'B' ?

 

And in general when are the classes inside on each other is that always case when they are in the same file.

 

Maybe there is also some chapter in the book of apex that explains more about the classes and al the posibillities how to combine them and of course with that their methods and properties.

 

sorry for the long post I am new in all this and there lots of things that I am trying to connect in my logic.

 

Thanks.

mauro_offermannmauro_offermann

In general an inner class is used in the following contexts:

* When the inner class has semantic relationship with the class that contains it and is not directly related to other classes in the project. Eg:

 

public class Car {
	public vclass Engine {
		public Integer waterLevel {get; set;}
		public Integer oilLevel {get; set;}
		public Integer RPM {get; set;}

	... some engine methods...	
}
}  

 

* When you need to group fields in a structure:

 

public class CustomerManager {
	public class Customer {
		public String firstName {get; set;}
		public String lastName {get; set;}
		.. in general without methods ...
	}
}

 

 

A very important aspect that should not be confused with normal inheritance of classes NOT an inner class inherits the methods of the class that contains it. However if you use 'extends' you are inheriting the methods of the parent class.

To use an inner class you must instantiate it like a normal class, but in the following way:

    Car.Engine engine = new Car.Engine();

That is, you must prefix the name of the inner class with the name of the class that contains it (except if using the inner class within Car).

Sorry for my English, not my native language.

I recommend you look for a tutorial on object-oriented Java there is a lot of documentation and syntax is the same as in APEX.