• 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

Custom Exception

 
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I am trying to override ArrayIndexOutOfBoundsException with my custom exception NoArgumentException. The program compiles and runs but I am not getting the result as I want it. It must print "Please enter the argument at command line" if no argument is supplied at command line, but it is printing ArrayIndexOutOfBoundsException.

Please help

Please
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you are trying to extend Exception with your NoArgumentException. If you throw an ArrayIndexOutOfBoundsException and print it out, the class name is part of the printout.

When you create your own Exceptions you should consider what the purpose of your Exception will be and use that to choose the superclass. A NoArgumentException is not an ArrayIndexOutOfBoundsException, so I think that particular inheritance hierarchy is inappropriate. Also the name of the class, MyException is poorly‑chosen because it is not an Exception of any kind. ExceptionDemo might be a better name.

What happens if you require a content to args (as you are doing at line 35) and don't pass any arguments, you will be trying to get an element out of a 0‑element array. You would need to start the class with this sort of instruction (assuming you change it to ExceptionDemo)

java ExceptionDemo "chop suey" "spaghetti bolognese"

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you move the use of args[0] until after your test for args.length == 0
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Attached is the screenshot of the exception I get if no argument is passed at command line.

I changed the name as you suggested.

I have extended ArrayIndexOutOfBoundsException as well but it did not work. Kindly check




Error.jpg
[Thumbnail for Error.jpg]
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have corrected the code tags: the code goes after [code=java] and before [/code]

You don't appear to have taken any notice of my other two suggestions from earlier, however. Provide arguments or move the use of args[0]. Look at lines 35-36.
The command() and checkFood() methods do not throw any Exceptions: take the throws clauses off.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't override ArrayIndexOutOfBoundsException with your own custom implementation. Extending a class does not cause your subclass to be used instead of the original.

If you access an array index that is out of bounds you get an ArrayIndexOutOfBoundsException, and you can't change that without rewriting the Java API.

What you can do is catch the Exception that is thrown, and the rethrow your own type.



Be sure to make sure your new exception can accept a Throwable as a constructor parameter, and pass that to the super constructor in Exception. That will add it as the cause of your custom Exception, and maintain the full stack trace.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!! It worked.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic