You need to sign in to do that
Don't have an account?

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');
}
}
}
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:
Class that invokes it and catches the exception:
The debug prints out for me the I Got an exception message
All Answers
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:
Class that invokes it and catches the exception:
The debug prints out for me the I Got an exception message
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