• 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

Programmatically Thrown Exceptions

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
From my understanding "Programmatically Thrown Exceptions" must be declared or catched, but why does the code as following can be compiled:

1. class B {
2. public static void main (String[] arg)
3. {
4. Integer i=Integer.valueOf("123");
5. }
6. }

In the class "Integer", the method "valueOf" is declared as ---
public static Integer valueOf(String s)throws NumberFormatException.

why, in the caller code, can it be complied without exception thrown or captured ? Thanks.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two kinds of exceptions in Java: checked and unchecked exceptions. For checked exceptions, you are required to handle them - either by adding a try-catch block, or by adding 'throws SomeException' to the method declaration. For unchecked exceptions, this is not required.

NumberFormatException is an unchecked exception, so you are not required to handle it.

For more info, see: The Java Tutorial: Exceptions

Note that programs can both throw checked and unchecked exceptions. So your term "programmatically thrown exceptions" does not correspond to "checked exceptions".
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Haibo

NumberFormatException extends (indirectly) RuntimeException, wich is unchecked exception.

Alexsandra
 
Haibo Jia
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it, thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic