Exception chaining cannot work as long as it's legal to throw a non-Throwable.
(There is nothing to chain to!)
Even without exception chaining, this also creates a great deal of needless complexity in the runtime.
This should be rejected at compile time:
void main()
{
throw new Object;
}
Comment #1 by clugdbug — 2011-01-13T07:32:49Z
PATCH: Straightforward. Only non-obvious feature is that NULL as the type in a catch statement needs to become catch(Throwable) not catch(Object), so that scope statements will work.
After making this change, a couple of changes (about four?) are needed to druntime and phobos. All are of the form:
catch(Object o)
{
...
throw o;
}
Just need to change Object to Throwable in each case.
Now probably, it would make sense to also disallow catch(Object).
This would be done with the same code as in ThrowStatement::semantic.
-------------
Index: aggregate.h
===================================================================
--- aggregate.h (revision 871)
+++ aggregate.h (working copy)
@@ -199,6 +199,7 @@
{
static ClassDeclaration *object;
static ClassDeclaration *classinfo;
+ static ClassDeclaration *throwable;
ClassDeclaration *baseClass; // NULL only if this is Object
#if DMDV1
Index: class.c
===================================================================
--- class.c (revision 871)
+++ class.c (working copy)
@@ -31,6 +31,7 @@
ClassDeclaration *ClassDeclaration::classinfo;
ClassDeclaration *ClassDeclaration::object;
+ClassDeclaration *ClassDeclaration::throwable;
ClassDeclaration::ClassDeclaration(Loc loc, Identifier *id, BaseClasses *baseclasses)
: AggregateDeclaration(loc, id)
@@ -180,6 +181,12 @@
object->error("%s", msg);
object = this;
}
+
+ if (id == Id::Throwable)
+ { if (throwable)
+ throwable->error("%s", msg);
+ throwable = this;
+ }
//if (id == Id::ClassInfo)
if (id == Id::TypeInfo_Class)
Index: statement.c
===================================================================
--- statement.c (revision 871)
+++ statement.c (working copy)
@@ -4210,7 +4210,7 @@
sc = sc->push(sym);
if (!type)
- type = new TypeIdentifier(0, Id::Object);
+ type = new TypeIdentifier(0, Id::Throwable);
type = type->semantic(loc, sc);
if (!type->toBasetype()->isClassHandle())
{
@@ -4435,8 +4435,9 @@
#endif
exp = exp->semantic(sc);
exp = resolveProperties(sc, exp);
- if (!exp->type->toBasetype()->isClassHandle())
- error("can only throw class objects, not type %s", exp->type->toChars());
+ ClassDeclaration *cd = exp->type->toBasetype()->isClassHandle();
+ if (!cd || ((cd != ClassDeclaration::throwable) && !ClassDeclaration::throwable->isBaseOf(cd, NULL)))
+ fd->error("can only throw class objects derived from Throwable, not type %s", exp->type->toChars());
return this;
}
Comment #2 by clugdbug — 2011-01-13T07:47:51Z
(In reply to comment #1)
> Index: statement.c
> ===================================================================
> --- statement.c (revision 871)
> +++ statement.c (working copy)
> @@ -4435,8 +4435,9 @@
> #endif
> exp = exp->semantic(sc);
> exp = resolveProperties(sc, exp);
> - if (!exp->type->toBasetype()->isClassHandle())
> - error("can only throw class objects, not type %s",
> exp->type->toChars());
> + ClassDeclaration *cd = exp->type->toBasetype()->isClassHandle();
> + if (!cd || ((cd != ClassDeclaration::throwable) &&
> !ClassDeclaration::throwable->isBaseOf(cd, NULL)))
> + fd->error("can only throw class objects derived from Throwable, not
> type %s", exp->type->toChars());
> return this;
> }
Sorry, the fd->error(...)
should be error(...)