• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Exception handling throw and throws

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please explain the difference between Exception handling throw and throws clause?
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am now clear on throw clause can you please explain more on throws clause....???
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'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
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic