GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 482 of 502    Print  
difference between throw and throws
What exactly is the difference between between throw and throws? If both are used for the same purpose then why are both needed instead of one?


  
Total Answers and Comments: 2 Last Update: June 30, 2008     Asked by: Kartheek11 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: krishna_kanth83
 
they are not same.

"throws" declares that your method is capable of throwing an exception.

"throw" actually  does the work ,  of throwing the exception.

example :

public void doSomething() throws ApplicationException
{
    try{
        }
    catch(Exception e){
            // catch all excpetions n provide some meaningful message
            throw ApplicationException("An error occurred while trying connect to DB");
        }
}

Above answer was rated as good by the following members:
interviewprep9, dhivyaid, r.praveenkumar, dabir, Naveenjavasun, timmireddy, pravinsharma, tanzeem khan, sushilkotnala, sonalgauravmahajan, bino75
June 14, 2008 11:04:08   #1  
krishna_kanth83 Member Since: May 2008   Contribution: 3    

RE: difference between throw and throws
they are not same.

"throws" declares that your method is capable of throwing an exception.

"throw" actually does the work of throwing the exception.

example :

public void doSomething() throws ApplicationException
{
try{
}
catch(Exception e){
// catch all excpetions n provide some meaningful message
throw ApplicationException("An error occurred while trying connect to DB");
}
}

 
Is this answer useful? Yes | NoAnswer is useful 6   Answer is not useful 1Overall Rating: +5    
June 15, 2008 08:31:11   #2  
vegetto Member Since: June 2008   Contribution: 10    

RE: difference between throw and throws
hi there

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 */
}


this is called as HANDLE OR DECLARE RULE i.e either u handle exceptions using try catch block or by declaring them



 
Is this answer useful? Yes | NoAnswer is useful 9   Answer is not useful 0Overall Rating: +9    

 Related Questions

Design Pattern means how u r approaching the problem.Architecture means how the data is flowing in your application 
Latest Answer : design pattern howarch what ...
Read Answers (4) | Asked by : Praveen

Latest Answer : JAVA is platform independent while .Net works only for the MS platforms. .Net supports an array of languages while for working with J2EE you have only one option and that is 'JAVA". .Net works for IIS/MTS server only but Java is supported by ...
Read Answers (7) | Asked by : Beena

What is the difference between an object and an instance? And give me a real time example to differentiate these two?
Read Answers (9) | Asked by : Beena

br />1.what is oracle.2.what is major differenece oracle8i and oracle9i.4.tell me some thing ur self.5.please tell me about oops.6.what is single inheritance.7.what is multiple inheritance.8.can java support 
Latest Answer : 8.can java support multiple inheritance.java support multiple inheritance for interfaces not fro classes25.what is difference between procedure and functions.function returns only one value where as procedure returns more than one value ...
Read Answers (11) | Asked by : rajjava

Latest Answer : for ur answer refer to text books,my question here, are session thread safe??, also execute method of the action class is thread safe??, i mean can multiple request process with in the execute method,.? pls email me to arun_hyd@msn.com ...
Read Answers (1) | Asked by : Beena

Latest Answer : HIWhat is meant by reusable? we can use instance of a class as we want more times. Then, and diff between bean and class.Prempremkumar.cm@gmail.com ...
Read Answers (2) | Asked by : Beena

Latest Answer : Swing has built in support for Look and Feel, but Awt has not it has to be get support from OS in which it is running. ...
Read Answers (5) | Asked by : Beena

Latest Answer : An Applet is a special kind of java program that is designed to transmit over internet and automatically executed by java compatible web browser. An Applet can react to user input and dynamically change.Application can be an execution of a program ...
Read Answers (4) | Asked by : Beena

Latest Answer : An interface is a specification of method prototypes. All the methods of interface are public and abstract. They are public because they should be available to third party vendors and they are abstract because their implementation is left for third party ...
Read Answers (11) | Asked by : prakash

What is the difference between Enumeration and Iterator?. When i can use Enumeration and when i can use iterator?. Can any one tell me the situation that only Enumeration could able to solve that problem and vice versa.
In Enumeration we cant modify the elements. where as in iterator we can modify the elements 


 Sponsored Links

 
Related Articles

Service Oriented Java Business Integration Review

Service Oriented Java Business Integration Review Introduction If you ve read through the texts which give you an introduction to SOA or Web Services you will often find them to be quite frustrating and the reason for this is because they spend too much time referencing business processes which are
 

Java and Client Server Models

Java and Client Server Models The Role of Client Servers on the Web Client server models provide the essential mechanisms for working with the Internet In fact most of the World Wide Web is built according to this paradigm In client server models the web browsers run by millions of users are the cli
 

What is difference between call by value and call by reference in function?

The arguments passed to function can be of two types 1. Values passed 2. Address passed The first type refers to call by value and the second type refers to call by reference. For instance consider program1 main() { int x=50, y=70; interchange(x,y); printf(“x=%d y=%d”,x,
 

The Interview Snafu

How to turn someone else’s mistake to your advantage Your dream job is about to become reality. A recruiter gave you the heads up about the perfect position at Humungous Conglomerate, Inc. You went through five interviews as well as a battery of psychological tests mandated by their HR de
 

Winning a Job Interview with a Winning Resume

Does your resume unlock your potential, take your skills to the highest level and win you the interview and the job you want now? The job market today is highly competitive and even if you think you have what it takes to get an interview you won’t get over the line without a polished, prof
 

Using UML with Java

Using UML with Java While Java is not a new language its application for the development of embedded systems is quite new Developers are beginning to take a second look at modeling languages such as UML and many feel it can be a powerful tool in their development arsenal mosgoogle center Introductio
 

Importance of Proper English during Job Interview

Importance of Proper English during Job Interview Your job interview is crucially important and it will determine whether or not you will get the job Depending on the type of job you re going for it is very important for you to use proper English In most cases jobs which offer higher salaries will h
 

Java Technology Trends

Technology Trends Involving Java Java is an object oriented programming language OOP for Web browsers It is organized around data rather than actions and supports polymorphism which allows the same code to be written generically so it can function with different datatypes inheritance which allows on
 

Difference between Scholarship and Grant

Difference between Scholarship and Grant While both scholarships and grants allow students to pay for their tuition without having to pay the money back there are a number of key differences between the two Knowing the difference between grants and scholarship will make it much easier for students t
 

HR Interview - HR Interview Mistakes You Will Want To Avoid

HR Interview Mistakes You Will Want To Avoid The job interview can be a stressful process This is especially true for those who are going after a competitive position Your nonverbal communication combined with the answers you give during the interview will determine if you are hired mosgoogle While
 

About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape