• Post Reply Bookmark Topic Watch Topic
  • New Topic

2 basic Exception related questions  RSS feed

 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two questions related to Exceptions based on the following code,



Question 1 : the code throws tExc is only required if the type of exception I am throwing ins't RuntimeException or Error, right?

Question 2 : The code doesn't compile. I get the following two error,



But Exception is a sublcass of Throwable, then why they won't accpet tExc as Throwable.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi, it compiles and runs fine, for me. Are you sure that's your complete code? How are you invoking the compiler?

It won't make any difference to your program, but Java class names should always start with an upper-case letter, like "TExc" and "Test," instead of "tExc" and "test."
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 1 : the code throws tExc is only required if the type of exception I am throwing ins't RuntimeException or Error, right?


Correct. RuntimeException and Error are unchecked exceptions and don't need to be caught.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you (or did you ever have) have a class of your own named Exception?
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Do you (or did you ever have) have a class of your own named Exception?

Darryl, you just blew my mind with that question. I added such a class to Quazi's program and, indeed, it causes the errors he is getting. But... how is that possible? I even added a class called "Object" and that compiled fine. Is that a case of masking?
 
Paul Clapham
Sheriff
Posts: 22816
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call your classes anything you like, and you can put them into any package you like... except for java.lang. So you can create a class called Exception, but you can't create a class called java.lang.Exception.

Your Java code automatically imports everything in the java.lang package without you having to code that, so "Exception" automatically means "java.lang.Exception"... except when your class isn't in a package and you also have a class called Exception which isn't in a package. In that particular case "Exception" means your packageless "Exception" class.

Which is why it isn't a good idea to write classes with the same name as commonly-used classes from the standard API. It's legal to do that, as I said, but it can lead to confusion.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Your Java code automatically imports everything in the java.lang package without you having to code that, so "Exception" automatically means "java.lang.Exception"... except when your class isn't in a package and you also have a class called Exception which isn't in a package. In that particular case "Exception" means your packageless "Exception" class.


Thanks, Paul. I never think about java.lang being automatically imported. Now I have a couple of questions of my own. If I put my code into a package, I can still get the compiler errors Quazi reports:



So is there any distinction between defining my own Exception class in a package and defining my own Exception class in no package?

Second, if I explicitly import java.lang.Exception, I get a compiler error:



Why does the compiler let me off the hook, despite the clash with java.lang.Exception, when I rely on automatic import, when it doesn't let me off the hook after an explicit import?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will compile.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote: . . . you can put [classes] into any package you like... except for java.lang. . . ..
I think that is any package whose name starts java…
It is actually one of the conditions when you download the JDK, so you can check the exact terms there.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:This will compile.


That is nothing short of a, "first step into a larger universe" moment.

For that, I bestow upon you, my first Cow.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Stevens. I became aware of the use of fully qualified names to disambiguate in the context of java.util.List and java.awt.List. You can import only one (either specific import or import-on-demand) and use a fully qualified name for the other. If however you havethen you have to use the fully qualified name for both.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same applies to classes called Timer and I think Date.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:If I put my code into a package, I can still get the compiler errors Quazi reports:



So is there any distinction between defining my own Exception class in a package and defining my own Exception class in no package?


The compiler tends to take the easy route. It looks for a class that matches the one mentioned starting with the current file or imports, then the classes in the same package then java.lang.

Stevens Miller wrote:
Second, if I explicitly import java.lang.Exception, I get a compiler error:



Why does the compiler let me off the hook, despite the clash with java.lang.Exception, when I rely on automatic import, when it doesn't let me off the hook after an explicit import?


This falls foul of the first part of the order I gave above:
"...starting with the current file or imports..."
They hold the same precedence (after all, it has to compile the code given), in the same way that two imports of different classes with the same name hold the same precedence.
So it can't tell which one you mean.

In the second example if your Exception class was in its own file rather than a local class in the test file then it would compile, because your import statement has told the compiler "this is the Exception class I want to use".

Hopefully that made some sense...:)
Rattled out a bit quickly.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote: . . .
The compiler tends to take the easy route. It looks for a class that matches the one mentioned starting with the current file or imports, then the classes in the same package then java.lang.
. . .
I thought it looked in the same package first, then java.lang, then the import statements last.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Dave Tolls wrote: . . .
The compiler tends to take the easy route. It looks for a class that matches the one mentioned starting with the current file or imports, then the classes in the same package then java.lang.
. . .
I thought it looked in the same package first, then java.lang, then the import statements last.


Nope.
With a dummy Exception class in stuff.pack2 and my test class in stuff.test:

This gives the "Required: Throwable, Found: blah de blah" error.

Import statements are you telling the compiler (and JVM) where to find the classes you want to use.
java.lang is a fallback, if you like.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stevens. I am surprised that it compiles. But it was supposed to.

I was using Sublime editor. Later I tried it command line.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!