This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes throws and throw Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "throws and throw " Watch "throws and throw " New topic
Author

throws and throw

Santosh Kumar Nayak
Ranch Hand

Joined: Aug 02, 2011
Posts: 95
Hi

Can we have throws and throw new Exception in the same method ?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

What happened when you tried it?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Santosh Kumar Nayak
Ranch Hand

Joined: Aug 02, 2011
Posts: 95
Basically I wanted to understand as under what circumstances shall we encounter compile time exception ?

similar to IOException ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Santosh Kumar Nayak wrote:Basically I wanted to understand as under what circumstances shall we encounter compile time exception ?

similar to IOException ?


First, do you know the difference between a checked exception and an unchecked exception?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Henry Wong wrote: . . . do you know the difference between a checked exception and an unchecked exception?

Henry
It's in the Java Tutorials.
Santosh Kumar Nayak
Ranch Hand

Joined: Aug 02, 2011
Posts: 95
Yes

Checked stands for compile time exception and

Unchecked stands for run time exception.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Santosh Kumar Nayak wrote:Checked stands for compile time exception and
Unchecked stands for run time exception.

No it doesn't.

What is true is that unchecked Exceptions will not normally generate compiler (ie, syntax) errors; but that has nothing to do with how they work. ALL Exceptions are thrown at runtime.

The difference is that checked Exceptions MUST be caught or thrown - ie, code that can throw a checked Exception must be either:
(a) in a try...catch block that catches the checked Exception, or one of its parents.
(b) in a method that has a throws clause that includes the checked Exception, or one of its parents.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Santosh Kumar Nayak wrote:Checked stands for compile time exception and
Unchecked stands for run time exception.

No.

Unchecked exceptions are any exceptions that extend java.lang.RuntimeException. But that does not mean that other exceptions are "compile time exceptions". There is no such thing as "compile time exceptions". Exceptions are always thrown when the program runs - at runtime. Exceptions in your program are never thrown when the compiler is compiling your source code (compile time). Don't let the word "Runtime" in RuntimeException confuse you.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Santosh Kumar Nayak
Ranch Hand

Joined: Aug 02, 2011
Posts: 95
Thanks Winston.

Actually I wanted the answer as you mentioned above.

"ALL Exceptions are thrown at runtime".

 
Consider Paul's rocket mass heater.
 
subject: throws and throw