• 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

throws

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, i dont understand about the throws modifier. for example, why do i have to throw java.lang.InterruptedException if i want to use thread in the method body?, why cant i use try and catch statement before i use thread in my code and try to catch the exception instead? can a method throw more than one exception? thanks
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

yes, a method can throw more than one Exceptions:

you dont need to declare the Exception if you catch it:

if you use


then you catch all subclasses of Exception.

k
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Syamsul Hussin:
hi, i dont understand about the throws modifier. for example, why do i have to throw java.lang.InterruptedException if i want to use thread in the method body?, why cant i use try and catch statement before i use thread in my code and try to catch the exception instead?


You don't have to throw the InterruptedException, you can simply catch it within the method. I don't know what makes you believe otherwise. For example:

Jeroen Oosterlaar
 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, if I declare a class to extend a super which throws an exception, do I have to throw the same exception again? thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic