Replace assert with check and add exception.
Verify cacert existence before using it.

Closes #3362.