• Post Reply Bookmark Topic Watch Topic
  • New Topic

out of scope expection  RSS feed

 
U Kvistborg
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one question, it actually came about when I saw some very baad code, but then I just had to see what happend so I made this example, but how can the exception ever be seen if it is out of scope for the catching class to see ?

My main class in one file and in one package.


My throwing class in another package but in one file I have both the thrower class and the runtime exception:



How can the main class actually see the out of scope exception ? I would have guessed that i'll had an out of scope sort of exception not that it actually prints the exception in the main and with the message text in this case just Hej.

[ Edited to use code tags - Paul Sturrock ]
[ January 17, 2007: Message edited by: Paul Sturrock ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced topic. Moving...
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but how can the exception ever be seen if it is out of scope for the catching class to see ?
Why do you believe the exception is "out of scope"?
 
U Kvistborg
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it has "default" access, only package can see it.
And the main is in a different package, so I can not catch the TestException only the Runtime exception.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Access specifiers (public, protected, private, package) of a class do not control where objects of that class may be handled. They control where direct reference can be made to the class.

So, if class Foo is package-private, then only classes in its package can refer to Foo. But classes anywhere can handle an object of class Foo, if they're given one. They can't have a variable or field statically typed to Foo, because they cannot refer to Foo. But they can have a variable or field statically typed to a superclass of Foo, and then assign an object that happens to be a Foo to them. They can invoke methods of the superclass on it, but not methods that are specific to Foo.

I'm not sure that I've explained this very well, but it important that you gain an understanding of it, as it is fundamental to object-orientation in Java.
 
U Kvistborg
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes thanks, now it is clear to mee :-)
Actually pretty stupid question in the first place, I see now, thanks alot.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!