• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throws and throw

 
Santosh Kumar Nayak
Ranch Hand
Posts: 143
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Can we have throws and throw new Exception in the same method ?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Santosh Kumar Nayak
Ranch Hand
Posts: 143
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically I wanted to understand as under what circumstances shall we encounter compile time exception ?

similar to IOException ?
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes

Checked stands for compile time exception and

Unchecked stands for run time exception.
 
Winston Gutkowski
Bartender
Pie
Posts: 10268
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Santosh Kumar Nayak
Ranch Hand
Posts: 143
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston.

Actually I wanted the answer as you mentioned above.

"ALL Exceptions are thrown at runtime".

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic