• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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.
 
What a show! What atmosphere! What fun! What a tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic