What is the difference between throw and throws clause, explain in programatically
Editorial / Best Answer
Answered by: sri
throw is used to throw an exception manually, where as throws is used in the case of checked exceptions, to reintimate the compiler that we have handled the exception. so throws is to be used at the time of defining a method and also at the time of calling that function, which rises an checked exception.
Prog. to explain diff. b/w throw and throw:
class MyException extends Exception //to create our own exception
public String toString() //overriding the method toString() to print the desired msg.
return "Can not divide a no. with one: "+"MyException";
public static void main(String args) throws MyException //use of throws
throw new MyException(); // rises an MyException, if we try to divide a no. with 1
if we want to rise our own exception, we have to use either throws or to handle the exception by try-catch. if not, it gives the compile time error.
and throw is to rise the exception manually, In the above prog. I rised an exception when you try to divide a no with 1.(own Exception)
- What is the use of inspect verb in cobol?
- Oracle cursors in PL/SQL
- Difference between active & reactive power meters
- Difference between 11i and r12
- Dataset stage and sequential file stage
- Recursive function
- What is the difference between trigger and constraints ?
- Explain white box testing related to Java ?
- What is the difference between pass data item value and pass parameter value ?
- What is the difference between page not found and page cannot be dislplayed
- What are the basic difference between re-insurance and insurance claim handling?
- Business rule & business validation
- What is the difference between subnetmask and host
- How to calculate interest for VAT delay payment and how to file the returns,from basic please explain me.
- Microstrategy differences
- Sequential file stage
- Mortgage account
- What is the difference between a Java developer and Java designer?
- Cash management service
- What qualifications/traits/skills to become a good tester