aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception handling throw and throws Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception handling throw and throws" Watch "Exception handling throw and throws" New topic
Author

Exception handling throw and throws

Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
Can someone please explain the difference between Exception handling throw and throws clause?
N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
http://java.sun.com/docs/books/tutorial/essential/exceptions

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.

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)


N.Senthil Kumar
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
I am now clear on throw clause can you please explain more on throws clause....???
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

'throws' clause is appended to function and tells the compiler, "Ok if this function throws any (checked/unchecked) exception, then just pass it on to the calling function, and let him handle this exception."

Example:


HTH


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Remember that if your method can throw any checked exception at any point and doesn't handle it, then it must declare that exception in its throws clause. This is not required for unchecked exceptions. Declaring any unchecked exceptions in throws clause is useless but allowed...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
Thanks a lot Sagar and Ankit ...but I still have a question


Output:-
This is a Kathy S book example it says that it will not compile.....
MyException.java3:reported exception MyException; must be caught or declared to be thrown
doStuff();

So in order to execute this program how can we actually fix it or implement throws clause....
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
Hi Sagar,
I read your program and Kathy S book....The book says that in the program code of yours if it throws a checked exception but you are not declaring it...
It goes followed by the following example:-
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Sonali Sehgal wrote:Thanks a lot Sagar and Ankit ...but I still have a question


Output:-
This is a Kathy S book example it says that it will not compile.....
MyException.java3:reported exception MyException; must be caught or declared to be thrown
doStuff();

So in order to execute this program how can we actually fix it or implement throws clause....


throws clause is used properly in this program. doStuff method throws MyException and has declared it in its throws clause. doStuff is called in someMethod(). But some method neither declared MyException in its throws clause nor catches it. That's why you are getting an error. The two possible solutions for this are



Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
Hi Ankit,

Thanks a lot now I understand the declaration and rules of throws clause..........I am now able to understand all the above programs........
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Sonali Sehgal wrote:Hi Sagar,
I read your program and Kathy S book....The book says that in the program code of yours if it throws a checked exception but you are not declaring it...
It goes followed by the following example:-


Re read my first post, particularly this line:
...then just pass it on to the calling function, and let him handle this exception
That's mean, calling function (doStuff()) must handle the exception which are declared in called functions (doMore()) 'throws' clause.

HTH
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling throw and throws
 
Similar Threads
Difference
Difference between throws and throw in Exception
throw and throws
Exception handling Not Clear yet
Throws and throw clause