Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throws

 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • 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
 
J.H.B. Oosterlaar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic