GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes throws exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "throws exception" Watch "throws exception" New topic
Author

throws exception

mick lynch
Greenhorn

Joined: Aug 17, 2008
Posts: 23
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

Joined: Jul 11, 2006
Posts: 143
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.


Studying for SCJP 6
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4572
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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).
 
GeeCON Prague 2014
 
subject: throws exception