Unreachable statement error

This is in relation to an earlier post https://getthekt.com/unreachable-statement-error/ . This post is an addition to the previous one as another possibility. As this error mentions, there is something to be executed, which is after we have a return statement. So the execution will return from the return statement and cannot ever reach the next line of code.

Error (Unreachable statement error)

This is a sample class for this code.

Unreachable statement error

Resolution

Remove or restructure your code to make sure you don’t have any code after the return statement.

But you can still have code statements after the return statement if you are doing some conditioning as below. If you notice below, there is a return statement in line 7 and there are statement after that as well. It works , because the program executes the conditions and then finds the next return statement.

Conclusion

I wrote this to list another reason for this error “Unreachable statement”. Hope this helps someone. Keep reading and sharing..


Comments

Leave a Reply

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