• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how compiler know its a checked exception

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As we know that if at any point some exception occurs in the code then an instance of Exception class (or its subclass) is thrown from that point. Now in case of checked exceptions these exceptions are checked at compile time.

So my question is the instantiation comes into picture at runtime while checked exception are checked at compile time , therefore no instantiation is there, so how compiler comes to know that it is a compile time exception.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Whether an Exception is checked or unchecked depends on its inheritance. So every Exception class has an inheritance tree attached, which the compiler can follow.
 
Man Sharma
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to JavaRanch

Whether an Exception is checked or unchecked depends on its inheritance. So every Exception class has an inheritance tree attached, which the compiler can follow.





Thanks Campbell,

I just want to know the process . When an exception occurs then an instance is thrown from that point. Now the question is how an instance can be created at compile time, instance can be created only at runtime. Compiler does not know what type of exception it just take that instance and then find out the its type using reflection then print the stack's trace on console.

I referred also jsl 1, 2 and 3 but I could not get it how it is actually done at compile time.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would have to go through the Java Language Specification and Java Virtual Machine Specification to ge answers to those questions. I suspect the correct answer is "depends on the implementation." Remember each Class object has a link to its superclass object, so it is easy to follow those links to the superclass. Also the javac tool decides which Exception is checked; I suspect you would find no difference in the bytecode between a try-catch for a checkd and an unchecked exception, but you can confirm that with the javap tool.
 
I need a new interior decorator. This tiny ad just painted every room in my house purple.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic