Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Some how nothing is being caught. Not sure, but it could be not understanding how exception really works. So I have a method that does everything including querying, than finally does an update to an opportunity record

public void createAlert(){
    try{
        for (List<OpportunityLineItem> oliList : [Select oli.id from OpportunityLineItem oli Where oli.OpportunityId = :oppId]){

        //some logic
        }   
        //some logic
        update updateOpp;
    } catch (System.StringException e) {
        system.debug('Exception===>' + e.getMessage());
    }                                                   
}

I am invoking this method from the develop console right now like this :

Alert opp = new Alert('aaaaaaaaa');
opp.createAlert(); 

I get this error:

11:36:07:147 FATAL_ERROR System.StringException: Invalid id: aaaaaaaaa

But it never caught the exception, so I never got the "Exception===>" debug message.

share|improve this question
1  
What does the constructor method of your Alert class look like? It could be the exception is happening there rather than in the createAlert function. –  Doug B Apr 23 at 16:01
    
Less likely, but possible, is if in DC your Apex Code log level is INFO or higher - then the debug message won't appear –  crop1645 Apr 23 at 16:05
1  
It appears to me, based on how you have your brackets are set up, including having logic within a for loop containing your Select Query, it could very well be that you could have an invalid ID exception thrown before it can be trapped by your catch block. You may need to set up the try-catch differently. –  crmprogdev Apr 23 at 16:19
    
As Doug mentioned, can you post the code for your Alert class? –  Richard N Apr 23 at 16:36
    
All the constructor is doing is setting the Id. This is what my constructor looks like: public OppAlert(ID opp){ oppId = opp; } –  MCHam Apr 23 at 16:37
add comment

1 Answer 1

up vote 4 down vote accepted

It's a little lazy, but instead of catching specifically the StringException, you could just catch Exception, which is a catch-all bucket:

} catch (Exception e) {
    system.debug('Exception===>' + e.getMessage());
}   

Is that sufficient for your needs?

Also, your constructor is expecting an Id - and you are not passing in a valid Id. Salesforce does check Ids for legitimate formats. Pass in something that will pass the test, such as 005000000000000000.

share|improve this answer
1  
... additionally, this catch and ignore pattern should be corrected. Not presenting a message of some sort to the user is generally considered bad practice. –  Mark Pond Apr 23 at 17:27
    
Yes, that's true. I was more focusing on switching from StringException to Exception. Adding a message would be good, but seems beyond the scope of this question, since I was just adjusting one line of the provided code. –  DavidSchach Apr 23 at 17:29
    
I think the "Also...." part of the comment is probably more valuable and may well warrant it being an answer on its own. There really is a type mismatch there. –  Mike Ginou Apr 23 at 17:31
    
I hear ya! There is nothing wrong with the answer, it's spot-on. Just wanted to throw it out there so that it did not go unsaid. :-) –  Mark Pond Apr 23 at 17:31
    
Well, initially I did use just the all-catch all Exception, but it catch anything. So I assume maybe I should try something more specific and see if it catches it. –  MCHam Apr 23 at 17:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.