Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Try / Catch confusion

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic