aspose file tools*
The moose likes Beginning Java and the fly likes throws Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "throws" Watch "throws" New topic
Author

throws

Syamsul Hussin
Ranch Hand

Joined: Feb 09, 2003
Posts: 59
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


----------------------------------<br />SCJP 1.4
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
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

Joined: Sep 12, 2002
Posts: 41
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

Joined: Feb 09, 2003
Posts: 59
Hi, if I declare a class to extend a super which throws an exception, do I have to throw the same exception again? thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: throws