Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

throwing exceptions

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look at the following code:


The output is:
Hello World
Finally
java.lang.RuntimeException
at PassA.throwIt(PassA.java:3)
at PassA.main(PassA.java:8)
Exception in thread "main"


I am surprised that the compiler did not complain about a couple of things:
1. Shouldn't the throwIt() method declare the RuntimeException like:


2. Shouldn't the main method either catch the exception or throw the exception since throwIt() is being called there?

Thanks for helping me out!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RuntimeExceptions (and Errors) are "unchecked" by the compiler. Try this with a regular "checked" Exception and see what happens.
 
Nina Binde
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool..Thank you. I got it!
 
The moustache of a titan! The ad of a flea:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic