Win a copy of Event Streams in Action this week in the Java in General 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

[Mock Exams] Java Exceptions

 
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?
 
Rancher
Posts: 4183
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: 65056
247
  • 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: 65056
247
  • 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: 65056
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right, the JVM terminates the thread from which the exception was thrown.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!