throws Exception in method

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 8 10:09:35 PDT 2014


On Thu, May 08, 2014 at 03:19:04PM +0000, amehat via Digitalmars-d-learn wrote:
> On Thursday, 8 May 2014 at 14:02:06 UTC, monarch_dodra wrote:
[...]
> >Keep in mind that D also has the concept of "Error". Both "Exception"
> >and "Error" derive from "Throwable".
> >
> >"nothrow" only means the function will not throw an *Exception*.
> >
> >"Error" can be thrown any time, from anywhere. They bypass the
> >nothrow, bypass destructor cleanup, and fly past "catch (Exception)".
> >
> >An Error is basically: "A critical Error has occurred. Salvage what
> >you can before dying."
> 
> I do not know the mistakes, at least not as you speak. I just read on
> http://dlang.org/errors.html errors.
> 
> You mean errors and exceptions are almost identical?

What he meant, is that in D there are two kinds of throwables:

	     Throwable
	    /        \
	Exception     Errors

The 'nothrow' attribute means that the function will not throw an
Exception. But it does not guarantee that it will not throw something
else, like an AssertError (which is a subclass of Throwable but not a
subclass of Exception).

The intention is that "normal" exceptions must all inherit from class
Exception. Exceptions represent recoverable problems that can be handled
by catch blocks.

Other Throwable subclasses, like Errors, represent non-recoverable
problems with the program. E.g., AssertError means a wrong logic in the
program was detected by an assert statement, so the program is in an
inconsistent state that cannot be recovered from. So you should never
catch an Error. Or if ever you do, you should make sure you terminate
the program immediately afterwards. Also, note that some language
guarantees will not hold when inside an Error catch block, so it's a bad
idea to do anything other than emergency cleanup and then terminate the
program.


T

-- 
Genius may have its limitations, but stupidity is not thus handicapped. -- Elbert Hubbard


More information about the Digitalmars-d-learn mailing list