• 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

User-defined exceptions

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I get the following error when i run my program

java.lang.NoClassDefFoundError: 24
Exception in thread "main"

Below is my program, kindly advise where did i go wrong... thanks..

import java.util.*;

public class FactorException extends Exception{

public FactorException(int x,int y) {
System.out.println("Exception thrown : "+y+" is not a factor of "+x);
}

public static void main(String[] args) {
int n1 = Integer.parseInt(args[0]);
int n2 = Integer.parseInt(args[1]);
try{
factor(n1,n2);
}catch(FactorException e){
System.out.println(e);
}
}

static void factor(int a, int b) throws FactorException{
if(b%a == 0){
System.out.println(b+" is a factor of "+a);
}else throw new FactorException(a,b);
}
}

Regards
Shilong
 
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
Looks fine to me. Are you sure you're specifying the name of your class and not just passing the numeric arguments to "java"?

 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quote "Are you sure you're specifying the name of your class and not just passing the numeric arguments to "java"?"

Hi,

Pardon me for my ignorance.. i don't really understand your question.. you seem to get the same output for the first run also.. kindly advise further.. thanks...

java.lang.NoClassDefFoundError: 24
The number 24 seems to be pointing to my first command line input..


Regards
Shilong
 
Ernest Friedman-Hill
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 first run is a deliberate example of how to produce the error by typing the wrong thing -- I deliberately left out the name of the class, and you see how I get the same error as you. If I include the name of the class, as shown in the second example, it works.
 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a million, i got it..
 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested out my program and it exits once it catches the exception, but i need it to continue till the end of the program even after catching exceptions. Kindly advise how to....

import java.util.*;

public class FactorException extends Exception{

public FactorException(int x,int y) {
System.out.println("Exception thrown : "+y+" is not a factor of "+x);
}

public static void main(String[] args) {
int n1 = Integer.parseInt(args[0]);
System.out.println("Student id : T6194898");
try{
for(int g=1; g<args.length; g++){
int n2 = Integer.parseInt(args[g]);
factor(n1,n2);
}
}catch(FactorException e){
System.out.println(e);
}
}

static void factor(int a, int b) throws FactorException{
if(a%b == 0){
System.out.println(b+" is a factor of "+a);
}else throw new FactorException(a,b);
}
}

Thank you and regards
Shilong
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really understand what you mean. The catch you have is the end of the program. Your program seems to run fine for me.

P.S. Use code tags.
[ July 07, 2004: Message edited by: Darin Niard ]
 
Ernest Friedman-Hill
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
If you look at your code, you'll see that there's nothing after the catch block -- i.e., this program does continue to the end after catching the exception, there just isn't any more to do.

As a general pattern, if you want a loop to abort after an error, then you write



But if after an error, you want to continue with the next iteration of the loop, then you write, instead



In the latter case, the try/catch block is inside the loop; after the catch block finishes, the loop simply continues.
 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need the program to finish all the inputs from the command line even if the exception is caught in between the inputs...

i tried many conditions for the 'while' loop but it seems like no matter what conditions i use, the 'while' loop will repeat only the 'for' loop and the 'for' loop will still exit when the exception is caught... Any suggestions for the while loop condition???


Thank you!!!
 
Ernest Friedman-Hill
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
I've just used a "while" loop to illustrate the principle. You would do the same thing with your "for" loop -- i.e., move the try block inside of it. There's no need for you to add a "while" loop.
 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roger... i am kinda slow sometimes... it works now.. just that this error appears every time i run the program... it's abit too profound to me.. any advise...

java.lang.NumberFormatException: FactorException
at java.lang.Integer.parseInt(Integer.java:409)
at java.lang.Integer.parseInt(Integer.java:458)
at FactorException.main(FactorException.java, Compiled Code)
Exception in thread "main"


A million thanks........
 
Ernest Friedman-Hill
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
When parseInt() is passed an argument that isn't a String, it will throw NumberFormatException; you're not catching that anywhere, so the exception will be printed and the program will stop. Based on the error message, it looks like your program is getting "FactorException" as an argument -- i.e., you're running something equivalent to

java FactorException FactorException 1 2 3

which brings us back to your first post in this thread!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
When parseInt() is passed an argument that isn't a String, it will throw NumberFormatException;



"that isn't a String representing a number" is what you probably wanted to say, isn't it?
 
Ernest Friedman-Hill
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
It is indeed, my friend.
 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quote:

Are you sure you're specifying the name of your class and not just passing the numeric arguments to "java"?

code:
---------------------------------------------------------------------------

[ejfried@asllinux tmp]$ java 24 48 Exception in thread "main" java.lang.NoClassDefFoundError: 24
[ejfried@asllinux tmp]$ java FactorException 24 48 48 is a factor of 24

---------------------------------------------------------------------------

Roger, I totally understand your last thread, but I find this abit weird, previously when i wrote programs to accept inputs from command line, i do not have to include the name of the class like example two above and it still works.. but now i have to include the class name in the command line no matter what.. an explanation???
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I doubt it ever worked like that. You have to specify the class name so that the JVM knows what class it needs to run. How else would it know? Think about it.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps they were a c/c++ programmer in their previous life ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic