• 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

Exception handling Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

javachamp





1.error:
unreported exception java.io.IOException; must be caught or declared to be thrown
file.createNewFile(); // line 5

why am i getting this error ?
i think file format is correct ,then why this error.

2.if i uncomment the lines 18,19,11 and put the code throws IOException,then output is

java.lang.IllegalArgumentException

3.if i uncomment the lines 18,19 and put the code throws IOException,then output is

java.text.ParseException: Unparseable date: "12.11.2009"


can anyone explain why these different outputs are coming ?
 
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

mohitkumar gupta wrote:

1.error:
unreported exception java.io.IOException; must be caught or declared to be thrown
file.createNewFile(); // line 5

why am i getting this error ?
i think file format is correct ,then why this error.


Did you check the File API? and createNewFile() method?

mohitkumar gupta wrote:
2.if i uncomment the lines 18,19,11 and put the code throws IOException,then output is

java.lang.IllegalArgumentException


If you explicitely throw that(IllegalArgumentException), then that will be thrown. What surprise here?

mohitkumar gupta wrote:
3.if i uncomment the lines 18,19 and put the code throws IOException,then output is

java.text.ParseException: Unparseable date: "12.11.2009"


can anyone explain why these different outputs are coming ?



OK, If I asked, for creating an Integer object, what would be the output, if you code like below?

Will it work?
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:

2.if i uncomment the lines 18,19,11 and put the code throws IOException,then output is

java.lang.IllegalArgumentException

If you explicitely throw that(IllegalArgumentException), then that will be thrown. What surprise here?



i think output should be java.io.IOException since this IOException is automatically thrown when the line 10 is executed.


i know that java.lang.IllegalArgumentException is thrown following the above line,then what happens to the IOException .
Is IllegalArgumentException overwriting IOException .

if i just put the code throws IOException ,then also the output shows nothing.my view is

the line 20

should display

java.io.IOException

is the IOException not propogated down to main method ?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:i think output should be java.io.IOException since this IOException is automatically thrown when the line 10 is executed.



An IOException may be thrown, but only if there is a problem. If the file javachamp.dat is successfully created or already exists then no exception will be thrown.

mohitkumar gupta wrote:i know that java.lang.IllegalArgumentException is thrown following the above line,then what happens to the IOException .
Is IllegalArgumentException overwriting IOException .



If you get to the line of code that throws the IllegalArgumentException, then no IOException was thrown by the previous line (for the reasons given above).

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:
1.error:
unreported exception java.io.IOException; must be caught or declared to be thrown
file.createNewFile(); // line 5

why am i getting this error ?
i think file format is correct ,then why this error.

2.if i uncomment the lines 18,19,11 and put the code throws IOException,then output is

java.lang.IllegalArgumentException

3.if i uncomment the lines 18,19 and put the code throws IOException,then output is

java.text.ParseException: Unparseable date: "12.11.2009"


can anyone explain why these different outputs are coming ?


1. Please have a look at the File API and createNewFile method. Learning more about Checked and Unchecked exceptions would surely be of some help.
2. Thats because you explicitly throw it and catch it in your main method.
3. parse() is going to convert a String to a Date object. How does the java compiler know the String is in dd.mm.yyyy format, why not it be interpreted as mm.dd.yyyy format? So, it expects you to be more specific on how to parse the String input you give.
SimpleDateFormat is a concrete implemetation of the DateFormat class. Try this,

With this the compiler understands your format, parses and constructs a Date object accordingly - which is precisely what you want.
 
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

OK, If I asked, for creating an Integer object, what would be the output, if you code like below?

Will it work?




Yes it will work.
but this will say compile time error
 
Shanky Sohar
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
Parse method is not that much lenient about parsing dates.So it must be put in a try/catch block because it throws checked Parse exception
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanky Sohar wrote:

OK, If I asked, for creating an Integer object, what would be the output, if you code like below?

Will it work?


Yes it will work.
but this will say compile time error


Shanky, Abhimaran might have asked this to Mohitkumar, just to make him understand the compiler cannot just understand whatever we give it. Like... it will never know "one" is 1! Thats the point.
 
Abimaran Kugathasan
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

Vinoth Kumar Kannan wrote:
Shanky, Abhimaran might have asked this to Mohitkumar, just to make him understand the compiler cannot just understand whatever we give it. Like... it will never know "one" is 1! Thats the point.



Good catch! Thanks Vinoth Kumar Kannan!
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mohit,

I would suggest that you look at java api docs and see why IOException needs to be throws or declared.
You should have a clear concept of Checkd and UnChecked exceptions. Another thing, as long as your date
issue is concerned, you need to specify the pattern. Java won't automatically know the pattern you have provided.

Hope this helps,
 
Shanky Sohar
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

Abimaran Kugathasan wrote:

Vinoth Kumar Kannan wrote:
Shanky, Abhimaran might have asked this to Mohitkumar, just to make him understand the compiler cannot just understand whatever we give it. Like... it will never know "one" is 1! Thats the point.



Good catch! Thanks Vinoth Kumar Kannan!




Yes you are right,compiler will never understand one is 1.
but the thing is that.it can accept string to for Integer class.check API for Integer class
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html
reply
    Bookmark Topic Watch Topic
  • New Topic