• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt in throw and throws!!!  RSS feed

 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok,my doubt is why we use the throws keyword with the method,i know that it shows that which type of exception does method throw,but i have seen code as such

try
{
throw new NullPointerException
}

now here in this code we are using the keyword throw as well as new keyword in side the try block,then what is the difference between using throw and throws.

If anyone could explain me this it will be realy helpful thanks in advance...
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dhwani mathur:

try
{
throw new NullPointerException
}


I could not make much out of it.

You can have runtime exception anywhere in the code without the method having to declare it with throws.

But if its a checked exception , then either you have to take care of it using try , catch or have the method declare it using throws clause.
 
pitambari parekh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"throw" and "throws" are two different keywords..
When you use "throws Exception" , you are basically indicating that you are not catching the exception in that method and it should be thrown to the calling method and you must write your catch in that calling method.

When you use "throw new Exception()" , you are explicitly throwing an exception which has to be caught. You basically use this when you dont want to reach to a particular condition.Means if that particulr condition occurs "throw new Exception()" .....
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rahul,
thats ok,but i am not interested in knowing about checked exceptions but whats the difference between throw and throws why we use this keywords separately.I think there must be some significance.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks pitambari parekh for your explanation.
i got my doubt cleared.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the previous response by Mr. pitambari parekh.It explains a lot.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello dhwani,


now here in this code we are using the keyword throw as well as new keyword in side the try block,


throw Vs new - as its required to throw some exception object and you are creating a new object on the fly.

Thats why you use the new operator. Hope it does not offer further confusions


then what is the difference between using throw and throws.


Please look at this thread which explains with an example.

HtH.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raghavan
The link was wonderful... it helped me a lot..
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Pleasure dhwani.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!