Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

throw and throws  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
throw - is used to generate the exception. ok but what is the need of generate any exception?
throws - move the exception in the calling environment.

I'm not getting what is that? Please someone help.
 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there

Keyword "throws" is used in the calling method like:


Notice such method DOES NOT have try/catch. If the reading or writing the file goes wrong, IOException is thrown by the JVM and the exception is propagated to its calling method where it is handled.


Keyword ""throw" allows the programmer to wrap the original exception to something else (usually more user friendly) or re-throw the same exception:


This time when the DB fails to connect, the JVM will throw SQLException. However, you don't want the end user to know this is "SQLException" so you use a more specific "DBConnectionException". The DBConnectionException would be your custom-made exception:



 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at Oracle's Java Tutorials: Lesson: Exceptions

It answers your first question and explains exactly how exceptions work.
 
Vikram Bhati
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, Is there any difference between re throw any exception and throws? I think both are used to propagate the exception in the calling environment.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikram Bhati wrote:So, Is there any difference between re throw any exception and throws?

Yes, and I believe it's been explained already. throws is simply a method declaration; re-throwing is a conscious decision that you implement in code.

I think both are used to propagate the exception in the calling environment.

Have you read the link that Jesper suggested?

Either way, if you'd like a tip: Let your program throw Exceptions exactly as they occur, unless (and until) you come up with an alternative strategy. Now that may involve including throws clauses in your methods, but it doesn't involve try...catch blocks.

[Edit] Unless it involves things that require them, like ReentrantLocks.

HIH

Winston
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
throw is primarily used to raise user defined exceptions.
throws is added with signature of a method.Any calling method should handle expections mentioned with throws
 
Vikram Bhati
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the solution.
java default handler can handle only RuntimeExceptions but what about others like IO and Interrupted.
--> And Solutions is IO and Interrupted exceptions are handled manually using throw and throws.
You can try this code


now change throw new IOException("demo") to any RuntimeException for example throw new NullPointerException("demo") and Default handler handles this at runtime
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikram Bhati wrote:I got the solution.
java default handler can handle only RuntimeExceptions but what about others like IO and Interrupted.
--> And Solutions is IO and Interrupted exceptions are handled manually using throw and throws.

That's not exactly right...

A "throws" declaration for a method just means that you tell the compiler that this method might throw a certain kind of exception. When a method has "throws SomeException" in its declaration, it means that the method does not handle the exception - instead, it will pass the exception to whatever method called this method. It's then up to the calling method to decide what to do with it - either catch it, or throw it up to the next higher level in the call hierarchy. It is not so that Java's default handler "can handle only RuntimeExceptions".

Checked exceptions are exceptions for which the compiler will check if your code handles them correctly. For every checked exception, the compiler will inspect your code to check if you deal with it correctly. You can do one of two things with a checked exception inside a method: (1) catch it and implement whatever logic is necessary to deal with the exception, or (2) specify with a "throws" declaration that the method does not handle the exception, and will leave up to the calling method what to do with it.

For unchecked exceptions, the compiler does not do these checks. Methods can throw unchecked exceptions without explicitly specifiying that they might throw such an exception in a "throws" clause.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!