• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why we can't assign a Exception type variable to a Object type variable ?  RSS feed

 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean compiler complains when i compile this code.Why ?
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would it mean to catch an Object? You can't throw an Object, only Throwables. And you can't catch what isn't thrown. You can only throw and catch direct and indirect sub classes of java.lang.Throwable. That's just how the language is designed.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:What would it mean to catch an Object? You can't throw an Object, only Throwables. And you can't catch what isn't thrown. You can only throw and catch direct and indirect sub classes of java.lang.Throwable. That's just how the language is designed.


So that means, consider this as a exception in concepts of java because as i know i learn we can assign any of subclass type variable to a parent class type variable.
and Object class is a parent class of Throwable class and Throwable class is Parent of Exception class.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't just trying to assign the reference to an Object reference. If that were all you were doing, the compiler would have no problem with it. But you are trying to do this in a catch statement. And in a catch statement, special rules apply, as Rob said.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:You aren't just trying to assign the reference to an Object reference. If that were all you were doing, the compiler would have no problem with it. But you are trying to do this in a catch statement. And in a catch statement, special rules apply, as Rob said.

Okay, i got it . Thanks a lot Mike, Thanks a lot Rob
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:What would it mean to catch an Object? You can't throw an Object, only Throwables. And you can't catch what isn't thrown. You can only throw and catch direct and indirect sub classes of java.lang.Throwable. That's just how the language is designed.

I think it is not only about the catch,
all the keywords of exception handling in java can only be worked with the class of Throwable Type means with the Throwable or its sub type.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? What about try and finally, which work just fine even if no exception is thrown at all? Only catch is dependent on there being a Throwable involved somewhere.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Really? What about try and finally, which work just fine even if no exception is thrown at all? Only catch is dependent on there being a Throwable involved somewhere.


I think he means that where we specify a type, we can only specify Throwable and its subclasses, not Object, in line with his Throwable-as-subclass-of-Object confusion that prompted this thread. For example:



@Nikhil Sagar: If this is what you mean, then, yes you are correct.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's true that both catch and throw need to deal with a Throwable object - not just the catch. By my count, that's 2 out of the 4 keywords associated with exception handling.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does throws, surely. Three down, one to go!
[edit]the grammar in the JLS is not definite about throws, but it does appear to require Exceptions here.[/edit]
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Mike Simmons wrote:Really? What about try and finally, which work just fine even if no exception is thrown at all? Only catch is dependent on there being a Throwable involved somewhere.


I think he means that where we specify a type, we can only specify Throwable and its subclasses, not Object, in line with his Throwable-as-subclass-of-Object confusion that prompted this thread. For example:



@Nikhil Sagar: If this is what you mean, then, yes you are correct.

Yes, Exactly that is what i mean.
Thanks again jeff,
Thanks again Mike.
Thanks again Sherrif.

this answer was given by my teacher.
before that i gave him a explanation regardihg this like,
the subject of my topic is absurd it is "Why we can't assign a Exception type variable to a Object type variable ?"
actually we can assign like in this line

compiler does not complains at above line.
i thought the same thing happens at the time of exception handling with try-catch block .
i mean code inside try block produces the object of any exception type and if it happens then argument of the catch block catches that object's refrence id. like any method calling.
but here things are different, jvm not only creates the appropriate exception's object but also throw it that means i was thinking at the time of exception occurred the line should be like

but it is like :


and Either Throwable type variable or it's subclass type variable can handle this line because it is like :


So object type variables can't handle this line means.


here "Exception e" and "Object ob" are the argument of catch.
the above line is just for expressing my thoughts.i don't know if it's really happens or my confusion.
So, it is my request to all genius people here to see above explanation and tell me if i am wrong.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:So does throws, surely. Three down, one to go!

Ummm, that's three down, two to go. I forgot throws, but try and finally remain.

3 out of 3 people attempting to count this statistic have gotten it wrong. Including me

Campbell Ritchie wrote:the grammar in the JLS is not definite about throws, but it does appear to require Exceptions here.

Seems definite to me. Also commons sense tells us it's true, I think. I simply forgot "throws" entirely.
 
Bill Johnston
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add some confusion - I didn't notice anyone mentioning that you could do this (though of course I don't think you'd ever want to) ...

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Johnston wrote:Just to add some confusion - I didn't notice anyone mentioning that you could do this (though of course I don't think you'd ever want to) ...



Note, though, that that's just the same thing we've been talking about all along. Namely the target of a throw keyword has to be of type reference to Throwable.

(Also, just FYI, the cast to Object in the println() does nothing.)
 
Bill Johnston
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Bill Johnston wrote:Just to add some confusion - I didn't notice anyone mentioning that you could do this (though of course I don't think you'd ever want to) ...



Note, though, that that's just the same thing we've been talking about all along. Namely the target of a throw keyword has to be of type reference to Throwable.

(Also, just FYI, the cast to Object in the println() does nothing.)


Oh absolutely, I concur on all counts. My intent was just to demonstrate to the OP that you can cast back to Object from Exception, but you MUST cast to Exception (if that's what the Object really is) if you want to throw or catch it.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote: . . . 3 out of 3 people attempting to count this statistic have gotten it wrong. Including me
Are you sure about that, since everybody knows 87% of all statistics are made up on the spot
Mike Simmons wrote:
Campbell Ritchie wrote:the grammar in the JLS is not definite about throws, . . .

Seems definite to me. . . .
By “grammar” I meant the last section of the JLS, headed syntax, where it doesn’t say anything about Exceptions after throws. The link I quoted, however, does require something like ExceptionTypeList after the throws keyword. It might be that the exception type list, requiring the list be of exception type, is part of a context-sensitive grammar and they have had to massage that §18 into a context-free form.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that would be the second grammar - there are two. The first is distributed throughout most of the preceding chapters, including the section you actually quoted. The part that says

tells us that the throws clause is followed by a list of things called ExceptionType, which are class types or type variables. That's as clear as the grammar ever gets - we still need an external rules to tell us that yes, the thing called ExceptionType must be a subclass of Throwable or it's a compile error. That's normal, as such a concept is not expressible in the grammar.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume that is why they call it something different in the §18 grammar; they have to remove the other rules and need to get it into a context-free form to parse.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure both syntactic grammars are context-free grammars. The first syntactic grammar is presented in parts, surrounded by additional rules that are not part of the grammar. Which seems confusingly similar to context, but isn't. The grammar is only the production rules, not all the additional text. The difference between the two syntactic grammars is that the first is designed to facilitate additional explanations, while the second is designed for parsing efficiency. It's curious that they chose to rename everything in the second, in such a way that it becomes impossible to directly apply any of the additional language rules from outside the grammar. I guess the task of doing something useful with the AST after parsing is left as an exercise for the reader.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is that it says in the JLS in the exceptions section, as mentioned previously.
Throws:
  throws ExceptionTypeList

ExceptionTypeList:
  ExceptionType
  ExceptionTypeList , ExceptionType

ExceptionType:
  ClassType
  TypeVariable
Agree. That looks context-free, as long as the additional rule about compile-time error if not subclass of Throwable does not constitute context. I thought it did constitute context, but I was obviously not quite right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!