• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throws exception

 
mick lynch
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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 ?
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • 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.
 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic