Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

JVM vs Programmatically Exception  RSS feed

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.

I'm studying about exceptions, and I have a doubt when forcing some situation to throw ExceptionInInitializerError
using a NullPointerException.

Looking the code that follows, I'd like to know, if throwing an ExceptionInInitializeErro (JVM exception) must be
considered a programmatically exception because of the NullPointerException that causes it.

If a question like this appears on the exam, could I answer with JVM Exception even the cause being a programmatically exception?



java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at exceptions.ErroInicializacao.<clinit>(ErroInicializacao.java:5)
Exception in thread "main"


Tks!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a programmatic exception because nowhere has a programmer written throw new ExceptionInInitializerError(). The question is whether the programmer controls the creation of the exception/error, not whether they caused it due to a mistake.

Similarly, NullPointerException is also thrown by the JVM.
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,
I was wrong when I said that NullPointerException was thrown programmatically.

The question is whether the programmer controls the creation of the exception/error, not whether they caused it due to a mistake.



So, if I throw my onwn created exception programmatically inside an initialization block getting an ExceptionInInitializerError,
caused by MyException, could I consider it as thrown programmatically?

Example:


Output:
java.lang.ExceptionInInitializerError
Caused by: exceptions.MyException
at exceptions.ErroInicializacao.getValueException(ErroInicializacao.java:10)
at exceptions.ErroInicializacao.<clinit>(ErroInicializacao.java:7)
Exception in thread "main"

Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case the MyException is thrown programmatically. The ExceptionInInitializerError is thrown by the JVM in response to an uncaught error occurring during initialisation.
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Matthew.

I understand now.

 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!