JavaDeveloper
JavaDeveloper

Reputation: 5660

Is there ever a case when IO exceptions are thrown?

I have never seen any example where an IO Exception during java io operation was thrown. Each and every time I have seen it being caught. Is it true that for all practical purposes it never needs to be thrown ? If my answer to my previous question is untrue, then under which real life scenario is it ever thrown ?

Upvotes: 0

Views: 58

Answers (2)

assylias
assylias

Reputation: 328735

Well yes, say for example a user just clicked on a "Show Orders" button and here is the code:

List<Order> orders = getOrdersFromDatabase();
showOrdersInTable(orders);

public List<Order> getOrdersFromDatabase() throws IOException {... }

Unfortunately, some stupid guy just decided to cut the power cable of the server for fun (OK: he just got fired). The server running the database is now unreachable.

Your code can either:

  • ignore the exception and your application will fail silently without anyone knowing what is going on
  • let the exception propagate to your main without ever catching it and your application will crash miserably
  • catch and handle the exception at an appropriate abstraction layer, where you can log the details of the error and warn the user: showPopup("Sorry, the server xyz can't be reach right now, do you want to retry or abort?");

Upvotes: 1

fge
fge

Reputation: 121780

When you talk about IOException you also talk about all exceptions inherinting it. Among them, for instance, there is FileNotFoundException (old file API -- ditch) but also ReadOnlyFilesystemException, NotDirectoryException, AccessDeniedException, etc etc (new file API -- use).

Therefore, yes, a lot of IOExceptions are thrown, in fact.

Upvotes: 1

Related Questions