1

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.

5
  • 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
    Commented Apr 23, 2014 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
    – cropredy
    Commented Apr 23, 2014 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
    Commented Apr 23, 2014 at 16:19
  • As Doug mentioned, can you post the code for your Alert class?
    – Richard N
    Commented Apr 23, 2014 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
    Commented Apr 23, 2014 at 16:37

1 Answer 1

4

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.

5
  • 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
    Commented Apr 23, 2014 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. Commented Apr 23, 2014 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
    Commented Apr 23, 2014 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
    Commented Apr 23, 2014 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
    Commented Apr 23, 2014 at 17:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .