Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

throws exception

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appologise for this half arsed question. , How come when you are working with file input/output you use the Throws IOException or similar to that.
LIke if you create a file that is already there I think that throws an exception. However there is no catch keyword with instruction what to do when an exception is throw. The program continues to run without you ever knowing an exception was throw ?
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IOException is an unchecked exception, which means the compiler won't guarantee that it is checked in you code (meaning you have a try/catch block). So you don't necessarily need to use try/catch, although it is good practice to do so. I hope that helps.
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eric Daly:
IOException is an unchecked exception, which means the compiler won't guarantee that it is checked in you code (meaning you have a try/catch block).



Wrong. IOException is a checked exception. Any subclass of Exception (or Exception itself) excluding RuntimeException and it's subclasses are checked exceptions. Any program using a method which throws a checked exception must either catch or declared to be thrown that exception.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijitha has already pointed out the error in the first part of the post.

Originally posted by Eric Daly:
So you don't necessarily need to use try/catch, although it is good practice to do so. I hope that helps.


It's generally not good practise to try/catch for unchecked exceptions, as their arising is usually an indication of a bug in the code and/or a wrong runtime environment, missing resources and the like.

Only in exceptional cases (no pun) it may be desirable to trap a runtime exception and continue execution, with or without information to the user.
 
Marshal
Posts: 74661
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Darryl Burke:
Only in exceptional cases (no pun)



It explains in the Java Tutorial that "Exception" is short for something like "exceptional circumstances" or something similar.

There are only a few RuntimeExceptions which might be worth catching, eg java.lang.NumberFormatException and java.util.InputMismatchException, because the error may be sorted out by repeating the input.
 
Campbell Ritchie
Marshal
Posts: 74661
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mick lynch:
I appologise for this half arsed question.



Not a half-arsed question at all. Read what it says at the top of the Beginner's Forum contents page.
 
Sheriff
Posts: 22568
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
There are only a few RuntimeExceptions which might be worth catching, eg java.lang.NumberFormatException and java.util.InputMismatchException, because the error may be sorted out by repeating the input.


I agree with NFE, but IME can be prevented by checking for hasNextXXX in Scanner, which is the only class that is using it. And if an exception can be prevented by checking first, I think you should prevent it.
 
Campbell Ritchie
Marshal
Posts: 74661
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take your point, Rob. I was thinking about keyboard input, but even that can be sorted out with the hasNextXXX methods.
 
Rob Spoor
Sheriff
Posts: 22568
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The general rule is this:
- unchecked exceptions can be prevented by checks (!= null, hasBoolean(), etc)
- checked exceptions are caused when something outside the JVM causes a problem (network disconnect, hard disk failure, etc); checks are usually not possible to detect such issues

There are bound to be exceptions to this rule, but in most cases it does hold.


And then there are Error and its subclasses which means the JVM cannot cope anymore and wants to crash (OutOfMemoryError, StackOverflowError, etc).
 
reply
    Bookmark Topic Watch Topic
  • New Topic