Use `ConfigurationError` class from `trac.config` rather than the more generic `TracError`.
Extract error message to a variable so that it can be reused in `except` clauses.
Trap exceptions when parsing the port number.
Trap exceptions when parsing the query string.
Reuse generic error message to simplify code.
Refactor code to improve readability.