Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Try / Catch confusion  RSS feed

 
Dirk Lombard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am working on code a contractor wrote and saw the following:

static Class _mthclass$(String x0)
{
return Class.forName(x0);
ClassNotFoundException x1;
x1;
throw (new NoClassDefFoundError()).initCause(x1);
}

I would thinks that it should look like this:

static Class _mthclass$(String x0)
{
try
{
return Class.forName(x0);
}
catch(ClassNotFoundException x1)
{
throw new NoClassDefFoundError(x1.getMessage());
}
}

Is the first set of code A way to handle exceptions in Java 5 or is it just something that I have been missing all this time?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The contractor code simply won't compile, for quite a few reasons. Maybe the "contractor's code" is actually code decompiled by a decompiler that couldn't quite do the job? This happens sometimes, that decompilers leave some stuff unfinished.

One thing that the contractor code does (or tries to do) differently is to use the ClassNotFoundException as the cause of the NoClassDefFoundError . The correct thing to do in your code would be "throw new NoClassDefFoundError(x1)" .
 
Dirk Lombard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. That cleared things up for me. I thought I was loosing it there for a moment.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!