Unreachable statement error

I was working on something and it was an old apex class. It was working fine as of now after the Sprint 18 upgrade. But, for some reason I created a new class with the same content. I started getting “Unreachable statement error”. I checked both the classes and they are exactly same, of course the name of the class was different. So, I was expecting this to be saved without any error.

Error (Unreachable statement)

The error I was getting is “Unreachable statement”. This was coming in the lines where I have return statements.

All seems to be good and I was confused about the error “Unreachable statement” as there was no more details about it.

Resolution

I noticed, if I change the code to remove the return statement it will save.

Salesforce Error

I also noticed the version of the class is 42 (Spring 18). So if you change the version to 41, it will resolve the error. Or you have to remove the return statement after the throw statement.

Conclusion

It was okay to use return statement after a throw exception statement earlier to API version 42, but from API version 42 , it is no more supported. As when the exception is thrown the control of the statement is already out to the caller and hence the return statement will no more execute or needed at that place.

Hope this will help if you are getting this error some how… Keep reading and sharing…


Comments

3 responses to “Unreachable statement error”

  1. in my case related with unreachable i just keep the same name of apex class and error get remove….plz make me correct if i was wrong

    1. Amit Sahu Avatar
      Amit Sahu

      Unreachable is not for class name for sure. It’s the error for lines of code after a return statement.

Leave a Reply

Your email address will not be published. Required fields are marked *