Javatpoint Logo

difference between throw and throws in java

By: discus*** On: Wed Jan 08 23:13:41 IST 2014     Question Reputation3 Answer Reputation0 Quiz Belt Series Points0  3Blank User
In which scenario we have to use throw and throws? how to decide we have to throw the exception by using throws or to handle the exception by using throw?Up0Down

 
throw is used to explicitly throw an exception. Wherever you want that exception occurs you can use throw.

throws is used with method signature. If in a method there occurs an exception you can provide throws with method signature which gives the user idea that there may occur an exception in this method.
Image Created0Down

By: [email protected] On: Thu Jan 09 10:52:06 IST 2014 Question Reputation0 Answer Reputation359 Belt Series Points0 359User Image
Are You Satisfied :6Yes4No
 
1.Throws clause in used to declare an exception and thorw keyword is used to throw an exception explicitly.

2. If we see syntax wise than throw is followed by an instance variable and throws is followed by exception class names.

3. The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature).

4.throws keyword cannot be used anywhere exception method signature while throw keyword can be used inside method.

Image Created0Down

By: [email protected] On: Thu Jan 09 12:54:39 IST 2014 Question Reputation0 Answer Reputation392 Belt Series Points0 392User Image
Are You Satisfied :3Yes2No
 
Hi Could you please provide the code for this.it's really helpful,Image Created0Down

By: [email protected] On: Thu Jan 09 15:00:06 IST 2014 Question Reputation5 Answer Reputation0 Belt Series Points2 7User Image
Are You Satisfied :1Yes1No
 
Thanks for all your replies.We can handle the exception or simply throw the exception by using throws. What my question is how to decide we have to use throw or throws? this is the question asked in one of my interview.Image Created0Down

By: [email protected] On: Thu Jan 09 23:14:38 IST 2014 Question Reputation3 Answer Reputation0 Belt Series Points0 3User Image
Are You Satisfied :1Yes1No
 
You use throws in a method signature to indicate that a method is capable of throwing a particular exception. Any code calling this method will have to deal with the possibility that that particular exception can occur.

The throw keyword is used to actually throw an exception. It can be used wherever you can place a statement.
Image Created0Down

By: [email protected] On: Fri Jan 10 11:50:12 IST 2014 Question Reputation0 Answer Reputation9 Belt Series Points0 9User Image
Are You Satisfied :0Yes0No
 
if the method throws IOException or some other exception, how to decide whether to use throws or throw

For eg,

public void readFile() throws IOException{

}
or
public void readFile(){
throw new IOException()
}

how to decide?
Image Created0Down

By: [email protected] On: Fri Jan 10 23:28:48 IST 2014 Question Reputation3 Answer Reputation0 Belt Series Points0 3User Image
Are You Satisfied :0Yes0No
 
Suppose i have developed one code ,and you want to take my code and use in your program ,then please tell me how u will u know that my code is going to generate an exception.So by declaring throws in my method u can conform that someones code is going to generate exception.So in next step u will Handel the exception.

Using Throw keyword u can throw exception manually.Suppose u have developed pension program in that program u r passing invalid age.At that time you have to throw exception is "U Hav entered Invalid Age "....

I hope u Got me...
Pravat Kumar Swain
[email protected]
Image Created0Down

By: [email protected] On: Wed Jan 15 09:11:00 IST 2014 Question Reputation-3 Answer Reputation0 Belt Series Points0 -3User Image
Are You Satisfied :1Yes1No