Showing posts with label exceptions. Show all posts
Showing posts with label exceptions. Show all posts

Tuesday, 22 July 2014

Java: arbitrary functional interfaces and checked exceptions in Java 8

The last couple of posts have covered how to handle exceptions with Java 8 functional interfaces. This post discusses how to handle arbitrary functional interfaces as of KλudJe 0.2.

Saturday, 19 July 2014

Java: lambdas, streams and IOException

The last post discussed exception handling with KλudJe in broad terms. This post presents a more practical example with the Stream type and I/O.

Sunday, 15 June 2014

Java: lambdas and easy checked exception handling with KλudJe

This post describes an approach to handling checked exceptions using Java 8 lambdas without adding try/catch blocks everywhere.

Wednesday, 22 August 2012

Java: checked exceptions and lambdas in Java 8 (pre-release)

Update: Java 8 has been released and I've implemented a library with a more elegant approach to exception handling than the one described in this post: details + downloads; source code; blog posts.


This post looks at the effect of lambdas on checked exception handling.

This code uses a pre-release Java 8 build. See the previous post for more.

Tuesday, 17 November 2009

Java: how to use an IllegalArgumentException

Calling my web log Illegal Argument Exception seemed like a clever idea at the time. It is probably just a recipe for confusing Java neophytes searching for their program errors. I should've listened to what my granny used to tell me about clarity, precision, and terseness when choosing identifiers.

To make up for it, here's a short post about IllegalArgumentException (the exception type).

Thursday, 2 October 2008

Java: how not to make a mess of stream handling

This article was written with Java 6 in mind.
Updated 2008/12/16.

  /**
   * Writes "Hello, World!" to a file.
   */
  public static void main(String[] args) {
    try {
      byte[] data = "Hello, World!".getBytes("UTF-8");
      OutputStream out = new FileOutputStream("output.txt");
      //if write throws an error
      out.write(data);
      //then close will never be called! BUG!
      out.close();
    catch(IOException e) {
      System.err.println("ERROR");
      e.printStackTrace();
    }
  }

If you aren't careful with streams in Java, you end up with resource leaks. This article addresses some of the pitfalls of stream handling.

The examples that follow use implementations of OutputStream. Which output stream is not important - implementation details are encapsulated by the object and may change between Java versions. It is the developer's responsibility to honour the contract as per the documentation.

OutputStream.close(): Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

New Java developers pick up pretty quickly that they have to close streams. Problems arise when they start thinking about how to do it.