• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

[Mock Exams] Java Exceptions  RSS feed

 
Ranch Hand
Posts: 121
  • 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: 4042
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: 121
  • 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: 64171
215
  • 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: 121
  • 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: 64171
215
  • 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: 121
  • 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: 64171
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right, the JVM terminates the thread from which the exception was thrown.
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!