• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why java forcing us to catch or throws checked Exception?  RSS feed

 
Arvind Porlekar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a question regarding throws and checked exception.

The checked exception are compile time exception. That mean you have to handle those exception at compile time.
So why java forcing us to handle those exception ?
another question is can i suppress the compile time exception ? if YES then what is use of handling compile time exception ?

in case of run time why java is not forcing us to handle exception?


Best Regards
Arvind Porlekar
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is compile-time checking of exceptions but there are no "compile-time exceptions". Compile-time checking of exceptions will result in compiler errors if you do not code around checked exceptions properly. There's nothing you can do to suppress or handle compiler errors except to fix the code. Compiler warnings can be suppressed with annotations but not compiler errors, as far as I know.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having checked exceptions in Java is a feature that some would argue should have been left out. Personally I'm slowly coming round to this idea. Here's an interesting read:

http://www.ibm.com/developerworks/java/library/j-jtp05254/index.html
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java has checked as well as unchecked exceptions. as Junilu said above there is nothing like "compile-time exceptions". unchecked exceptions are the one that are checked at compile-time. you will only get exceptions/errors when you run/execute the program. we call unchecked exceptions run-time exceptions which might have confused you to assume that there is something like compile time exceptions. always use checked/unchecked terminology. as far as your second part is concerned, yes you have to honor java's catch/handle or specify requirement. you cannot override that. it is an integral part of java language.
 
Kocha Sapam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions are *really* and *awfully* handy ;) you will need it.
You will find out why as soon as you write some code that could make errors.
I used to think that way too (when i began).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arvind Porlekar wrote:The checked exception are compile time exception. That mean you have to handle those exception at compile time.

Not quite. You have to handle it or declare the method as throwing it.

So why java forcing us to handle those exception ?

Because they thought it was a good idea and, with the major exception of IOException, I think they were right.

another question is can i suppress the compile time exception ?

See above.

if YES then what is use of handling compile time exception ?

See also above; but the main reason is that the error may be recoverable.

in case of run time why java is not forcing us to handle exception?

For the exact opposite reason - runtime errors are usually NOT recoverable.

HIH

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Because they thought it was a good idea and, with the major exception of IOException, I think they were right.


Winston, you might find this interview interesting, it sheds light on the opposite point of view: http://www.artima.com/intv/handcuffs.html
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Winston, you might find this interview interesting, it sheds light on the opposite point of view: http://www.artima.com/intv/handcuffs.html

Thanks Stephan. I think I actually read it a while ago, but I'm going to go through it again. Hopefully, even my dinosaur brain can re-adjust if it needs to.

Winston
 
Kocha Sapam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say you call a method somemethod(String paramstring) which takes a string as an argument for proccessing could cause some error like empty text parameter (taken from user input) then the exception could be thrown which you could then catch it in your code and take appropriate action like telling the user that its empty and ask to input again ;)

Java's really elagant way to handle errors

Just a simple example
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!