Improved error handling

Error handling for internal and external classed have been improved. Error
codes list is extended. More exception cases are covered.

Logging mechanism will be added in the next draft.
magic number "-1"