aspose file tools
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


Win a copy of Elasticsearch in Action this week in the Big Data forum!
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: 122
Hi

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

Joined: Oct 02, 2003
Posts: 11652
    
  17

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: 122
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: 19336
    
  41

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: 41070
    
  29
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: 122
Yes

Checked stands for compile time exception and

Unchecked stands for run time exception.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8626
    
  23

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: 14598
    
  24

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: 122
Thanks Winston.

Actually I wanted the answer as you mentioned above.

"ALL Exceptions are thrown at runtime".

 
Don't get me started about those stupid light bulbs.
 
subject: throws and throw