• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from Exam Lab (Exception Handling)

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1) Why the below code fails with "NumberFormatException". This exception is unchecked exception.So the user not require to handle it?

Q2) If i comment "throws Exception" from main clause it now throws InterruptedException.I know InterruptedException is a checked exception and user must provide try- catch block.But why it does not throw "NumberFormatException"? Why we get two different outputs if comment and uncomment throws clause from main clause?




 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NumberFormatException is a runtime Exception so it doesnot matter whther you throws it or not..........
but as interruptedException is a checked exception so compiler will checked whther the code which is calling the method is capable of catching or throws the exceptions.....
so if you mention as
public static void main(String args[]) throws Exception
that means if main method is capable of throws the InterruptedException........
so that will compiles fine ...........but if you removes throws Exception clause from the main method........
then you must have to put the try/catch or try/finally block around where the method is being called............
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

amolpalekar kadolkar wrote:Q1) Why the below code fails with "NumberFormatException". This exception is unchecked exception.So the user not require to handle it?



No matter, whether it's a checked or unchecked exception, if an exception happened in the code, it'll be thrown. But if it is checked exception, you must take certain actions(try/catch) to handle it, or declare the method as it throws an exception.

amolpalekar kadolkar wrote:
Q2) If i comment "throws Exception" from main clause it now throws InterruptedException.I know InterruptedException is a checked exception and user must provide try- catch block.But why it does not throw "NumberFormatException"? Why we get two different outputs if comment and uncomment throws clause from main clause?



Remove the throws clause, and run you above code. Did you able to compile it? Check it.
 
No, tomorrow we rule the world! With this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic