Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A question about Exceptions

 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem is that I get the compiler error " unreachable statement " in this code:




My question now is, why java generally prints the error on the statements, but not on the variables?

 
Roel De Nijs
Sheriff
Posts: 10763
148
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamil Hlubek wrote:My question now is, why java generally prints the error on the statements, but not on the variables?

And on which variable do you want this compiler error to appear in your code snippet?

The unreachable code is the print-statement on line24 (printing "6"). So that's the most obvious location to show the compiler error. It would be completely bonkers to show that error on for example line5 (String v = null;) as there is nothing wrong with that statement, it's a valid line of code.

Hope it helps!
Kind regards,
Roel

PS. Your ExceptionTest1 class is missing its closing curly brace.
 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my bad expression, but I ment something different ):

I ment, why such a error is only thrown if you write a statement after you threw an exception and not when you write an assignment of a variable after it, too.
 
Roel De Nijs
Sheriff
Posts: 10763
148
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamil Hlubek wrote:I ment, why such a error is only thrown if you write a statement after you threw an exception and not when you write an assignment of a variable after it, too.

This gives a compiler error on the assignment
If you add another statement in between the throw-statement and the assignment, you'll get a compiler error on this added statement as illustrated in this code snippetWhy? Probably because you'll get the error on the 1st statement that's unreachable.

Hope it helps!
Kind regards,
Roel
 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Kamil Hlubek wrote:I ment, why such a error is only thrown if you write a statement after you threw an exception and not when you write an assignment of a variable after it, too.

This gives a compiler error on the assignment
If you add another statement in between the throw-statement and the assignment, you'll get a compiler error on this added statement as illustrated in this code snippetWhy? Probably because you'll get the error on the 1st statement that's unreachable.

Hope it helps!
Kind regards,
Roel


Thank you for your help. Now I understand everything. (:
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!