This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

[Mock Exams] Java Exceptions  RSS feed

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While working on Mock Exams I stumbled upon this question:


My question is why the nr. 5 is not correct? Can you explain a bit more why nr. 5 is not correct?
 
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the VM wanted to exit the program then it would just exit.
It has no need to use Exceptions to achieve that and, indeed, since we (the developers) can catch and handle exceptions then the VM has no guarantee that throwing an exception will actually result in the program exiting.
 
John Losty
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But based on the exception(handled or not) the JVM will terminate or not?  
 
Marshal
Posts: 64473
225
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Losty wrote:. . . the JVM will terminate or not?  

Run the following code (on JShell or in a method):-Don't catch the exception. You will see that even though the “Main” Thread is terminated because of the uncaught exception, the frame remains visible showing the the Event Dispatch Thread (and therefore the JVM) is still running until you close the GUI.
 
John Losty
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running this:



So that means that even thought exception terminated 1 thread(or did it?) it didnt terminate the JVM. So therefore it cannot be the mechanism to exit the program when something unexpected happens.?

But what is than the mechanism that the JVM uses to exit the program? System.exit() ?
 
Campbell Ritchie
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Losty wrote:. . . even thought exception terminated 1 thread it didnt terminate the JVM.

No, an uncaught exception terminates the thread it was thrown in, but doesn't terminate other threads.

. . . But what is than the mechanism that the JVM uses to exit the program? System.exit() ?

Done't know. System#exit() is one of the options the user can use to stop the JVM, but it is probably not used internally.
 
John Losty
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

No, an uncaught exception terminates the thread it was thrown in, but doesn't terminate other threads.


I thought JVM terminates threads
 
Campbell Ritchie
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right, the JVM terminates the thread from which the exception was thrown.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!