StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Monday, May 26, 2014

Java 7 catching multiple exception types

 May 26, 2014     Java     No comments   

In Java 7 and later it is possible to avoid code duplication by using a single catch block to handle more than one type of exception. The bytecode generated for a single catch block handling multiple exceptions is smaller compared to an equivalent implementation with multiple catch blocks and thus considered better for performance.

Consider a simple example to read a file with numbers and create a list of numbers. In Java 6 and earlier, we do an implementation similar to the one below. Here we repeat the same exception handling code in each of the catch blocks.

public static List<Integer> readFile(String fileName) {
  List<Integer> myList = new ArrayList<Integer>();
  BufferedReader in = null;
  try {
    in = new BufferedReader(new FileReader(fileName));
        String line = null;
        while ((line = in.readLine()) != null)
        {
          int num = Integer.parseInt(line);
            myList.add(num);
        }
  } catch (NumberFormatException e) {
    // do some action
    e.printStackTrace();
  } catch (IOException e) {
    // do some action
    e.printStackTrace();
  } 
  finally {
    try {
      if ( in != null ) 
        in.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  return myList;
}
In Java 7 it is possible to specify a single catch block to handle multiple exceptions to eliminate duplicate code. Each of the exception types a catch block needs to handle is separated by a vertical bar "|". This implementation is considered efficient since the byte code generated is smaller.
public static List<Integer> readFileMultiCatch(String fileName) {
  List<Integer> myList = new ArrayList<Integer>();
  BufferedReader in = null;
  try {
    in = new BufferedReader(new FileReader(fileName));
        String line = null;
        while ((line = in.readLine()) != null)
        {
          int num = Integer.parseInt(line);
            myList.add(num);
        }
  } catch (NumberFormatException | IOException e) {
    // do some action
    e.printStackTrace();
  } 
  finally {
    try {
      if ( in != null ) 
        in.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  return myList;
} 
  • Share This:  
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Implement caching in a Spring Boot microservice using Redis
    In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Spring Boot with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Getting started with Kafka in Python
    This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
  • Getting started in GraphQL with Spring Boot
    In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

Copyright © StackStalk