• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling throw and throws

 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain the difference between Exception handling throw and throws clause?
 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I am now clear on throw clause can you please explain more on throws clause....???
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic