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
Eager-2-LearnEager-2-Learn 

How to catch a custom exception in the called class from the caller class???

Hi,

How would I write a try catch that needs to catch a custom exception that happens down stream from within a class method that I am calling?

 

For example, my called class would be something like this...

 

try {

  calledClass.mymethod;

catch ( ??THIS NEEDS TO TRAPPED THE CALLED CLASS CUSTOM EXCEPTION?? e) {

   ??HERE I WOULD LIKE A USER FRIENDLY MESSAGE??

catch (System.Exception e) {

   String FatalError = 'Whatever I want the user to see on the visualforce page';

   ApexPages.addmessage( new ApexPages.message(ApexPages.severity.FATAL, FatalError);

}

 

 

 

public  Calledclass {

   ...

   class CalledclassException extends Exception {}

   ...

   ...

   public myMethod {

      ...

      ...

      if ( some condition ) {

         throw new CallingClassException('ERROR IN CalledClass');

      }

   }

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

You need to make the Exception class public (or global if it's a managed package and being referenced from a class outside the package).

 

You can then reference it via the classname.classname notation. Here is a test sample:

 

Class that has a custom exception:

 

public with sharing class MyClass 
{
	public class CustomException extends Exception{}
	
	public static void testException()
	{
		throw new CustomException('This is a test');
	}
}

 Class that invokes it and catches the exception:

 

public with sharing class MyClassCaller 
{
	public static void execute()
	{
		try
		{
			MyClass.testException();
		}
		catch(MyClass.CustomException e)
		{
			System.debug('>>> I Got an exception');
		}
	}
}

 The debug prints out for me the I Got an exception message

All Answers

Sean TanSean Tan

You need to make the Exception class public (or global if it's a managed package and being referenced from a class outside the package).

 

You can then reference it via the classname.classname notation. Here is a test sample:

 

Class that has a custom exception:

 

public with sharing class MyClass 
{
	public class CustomException extends Exception{}
	
	public static void testException()
	{
		throw new CustomException('This is a test');
	}
}

 Class that invokes it and catches the exception:

 

public with sharing class MyClassCaller 
{
	public static void execute()
	{
		try
		{
			MyClass.testException();
		}
		catch(MyClass.CustomException e)
		{
			System.debug('>>> I Got an exception');
		}
	}
}

 The debug prints out for me the I Got an exception message

This was selected as the best answer
Raj.ax1558Raj.ax1558

Hello. 

 

yes you can handel custom exception. and shows different different messages depends on exception. like this - 

 

try

{

                // DML statements   / SELECT / any other code 

}

catch(System.DMLExcepton e){

//error msg

}

catch(System.EmailException em){

//error msg

}

 

catch(System.ListException lste){

//error msg

}

 

catch(System.QueryException  qrye){

//error msg

}

 

catch(System.NullPointerException nulle){

//error msg

}

 

For know more about exceptions click on this link - 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_exception_methods.htm

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other
benefits.

Thank You,
Raj Jha