• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

throw execption in try -catch

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can we throw user defined exception in try catch block ??

sth like:
try {
System.out.println("Hello " + args[0]);
}
catch (ArrayIndexOutOfBoundsException e) {
throw new Exception (" Error.");
}

Is it possible ?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it is possible.

cheers!
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it's ok. But you should understand the effect. You hand-over the exceptions to others and it would be like a snow ball. From the design point-of-view, it's ok if you throw the exception in try-catch block to your own exception class, not to general Exception.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

Yes, you can do that. But when you rethrow a checked exception you either have to declare in the method signature that the methods will throw this Exception.
Or you have to catch it back again, looks really ugly:

Compiles and prints out
start
main ready
java.lang.ArrayIndexOutOfBoundsException: 0
at Main.main(Main.java:5)




Yours,
Bu.
 
reply
    Bookmark Topic Watch Topic
  • New Topic