[SalesForce] String Exception is not being caught

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.

Best Answer

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.

Related Topic