An illegal opcode can happen when the program starts running code from RAM. Your program could do this if your stack has been corrupted or if you have overrun it. If the stack gets corrupted this means when a function returns it can return to somewhere unknown.
If you are writing code in assembler then you may have mis-alligned push and pull statements.
You can also get the same problem if you are using paging and have changed the code page, then when a function returns it will return to the correct address but on a different page.