Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exam lab q-9(excepton handling)

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

above code works fine.
but at line 1 ,run-time exception has never been thrown,so i guess it should be the error coz "i have read not to use the exception that has never been thrown in corresponding try block else compiler error".
please justify above statement.why it is not error?
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Arjun!

The RuntimeExceptions are not checked if they are thrown from the try block, because they are... unchecked.

They can occur or don't so there is no way you can say they will ever be thrown from the try clause.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pedro Kowalski wrote:Howdy
The RuntimeExceptions are not checked if they are thrown from the try block, because they are... unchecked.
They can occur or don't so there is no way you can say they will ever be thrown from the try clause.

howdy
that means RuntimeExceptions if used unnecessarily in catch will not harm.
but checked exceptions are evil in this case.
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just try it for yourself:



Will it compile?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pedro Kowalski wrote:Just try it for yourself:

Will it compile?


offcourse yes,works fine.
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope that helped mate :-)

Cheers!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Arjun! If the compiler checks whether the exception will be thrown or not, then that exception IS NOT a RuntimeException at all!
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic