Add tests and fix new fatal errors related to $/

In b3a2acfa0c0e4f8e48e1f6eb4d6fd143f293d2c6 I added new exceptions, but
forgot to test them properly. In the process I managed to partially break
the functionality, and since it was not tested I did not notice.

Ilmari on #p5p pointed out I forgot the test, and in the end I had to completely
rewrite the original patch.

Now tested as fully as I could. Thanks Ilmari.