You need to sign in to do that
Don't have an account?
jucuzoglu
Throwing a standard exception with a particular message
I use a try/catch block with code similar to the following to display a variety of exceptions including validation errors. Shown below is the catch poriton:
catch(Exception e) { ApexPages.addMessages(e); }
Now I would like to THROW a standard exception. i.e.
Try {
(Validate some condition)
}
catch (DMLException DMLe){
[How do I place code here that will provide a specific static message that i want to show as the particular exception message]
}
I don't think you can construct instances of standard Apex exceptions. You will have to wrap your message in a custom exception created by extending standard exception.
All Answers
I don't think you can construct instances of standard Apex exceptions. You will have to wrap your message in a custom exception created by extending standard exception.
If you are trying to push back a very specific / static message, you could have the different CATCH statements for different exceptions and do something like this:
Is that what you're looking for? Or are you looking to throw your own custom rolled exception?
-Andy
refer : http://boards.developerforce.com/t5/Visualforce-Development/How-do-I-properly-catch-and-display-error-messages-in/m-p/374773#M44448
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Custom extension was the way to go.
I created a custom exception class similar to this article which is the easiest I have read on the topic: http://th3silverlining.com/2009/06/11/throwing-custom-apex-exceptions/
Then after creating a custom extension I simply inserted a bunch of IF statements and inside used the code below
Worked well. Thanks to everyone for helping out.
public static void insertAccount() {
try {
//Account a=new Account();
//insert a;
throw new DmlException('Sorry insertion fails ');
} catch(DmlException e) {
System.debug('thrown standard DML exception '+e);
}
}
}
==>We can now extend exception class
refer: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_custom.htm
Mark this as best answer if it helps.