Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

what is difference between throw and throws in exception?

Answer Posted / chaitanya

throw:the throw keyword is used to throw the exception manually,that is when you feel a particular line in your code, when executed is capable of having some exception then you use throw keyword as:

throw new MyException(arguments if any);
this leads to instantiating of the MyException class and exception is thrown


throws: this is to be used when you are not using the try catch statement in your code but you know that this particular class is capable of throwing so and so exception(only checked exceptions).in this you do not use try catch block but write using the throw clause at appropriate point in you code and the exception is thrown to caller of the method and is handeled by it. eg:

void trouble()throws IOException,any other exceptions just separate them with commas.......
{
/* throw statement at appropriate step,
no need to use try catch here,exception would be thrown to caller and u should provide try catch block to handle exception there else this process cotinues further till
appropriately it is handeled or prog terminates abruptly */
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the significance of listiterator.

1173


How can you handle java exceptions?

1077


What is the use of toarray () in java?

1118


Can a constructor have different name than a class name in java?

1283


What are the various access specifiers for java classes?

1119


which one is more efficient int x; 1. if(x==null) 2.if(null==x) state which one either 1 or 2?

11392


What is byte data type?

1052


How do you detect memory leaks?

1115


Is ++ operator thread-safe in java?

1237


Why do I need to declare the type of a variable in java?

1154


What does flag mean in java?

1089


How to make a write-only class in java?

1106


Write code to implement bubble sort in java?

1050


How do you clear an arraylist in java?

1022


Differentiate between nested and inner class in java.

1050