Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
Igor Merabishvili
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any logic in the fact that FileNotFoundException is not a RuntimeException but NumberFormatException is?
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileNotFoundException is not a RuntimeException


Right, because, sometimes the file your using may not present or mistakenly deleted , so it always safe to confirm whether a file is present or not , Hence compiler forced us to handle it ( called checked exception )

but NumberFormatException is?


when you parse an integer or any primitive data type thru wrapper class static method like parsetInt , you know only numeric data is present in string and you can parse it.

Hope this help ..

Regards,
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The previous poster sort-of explained it, but without giving the background.

In general RuntimeException is for non-fatal[*] conditions that generally indicate a programming bug. For instance, dereferencing null reference, reading off the end of an array etc.

Checked Exceptions are for conditions that generally represent a problem with data or with the underlying system. For instance, a required disk file is not found (FileNotFoundException). Such conditions are typically not the direct result of a programming bug.

NumberFormatException is difficult to categorise, as it depends where the string being parsed comes from. If it comes directly from user input, a checked exception might be appropriate. If it was obtained programmatically, a RuntimeException might be appropriate. The Java designers picked a RuntimeException.

Don't lose sleep over it.
 
Igor Merabishvili
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you, guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic