DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Spring Transaction Propagation in a Nutshell
  • How to Convert XLS to XLSX in Java

Trending

  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • A Modern Stack for Building Scalable Systems
  • Solid Testing Strategies for Salesforce Releases
  1. DZone
  2. Coding
  3. Java
  4. Why You Should Avoid Using Exceptions as the Control Flow in Java

Why You Should Avoid Using Exceptions as the Control Flow in Java

Ever used exceptions as the control flow in Java? Well, you probably shouldn't. Here's why.

By 
Dursun Koç user avatar
Dursun Koç
DZone Core CORE ·
Updated Oct. 25, 19 · Tutorial
Likes (22)
Comment
Save
Tweet
Share
69.6K Views

Join the DZone community and get the full member experience.

Join For Free

Exceptions as control flow

Learn more about using exceptions as control flow in Java.

Java is a general-purpose programming language with many alternatives to solving a certain problem. However, there are best practices that need to be followed, and there are some bad practices out there as well that are still commonly used.

One of these common bad practices is using exceptions as the control flow. This should be avoided for two reasons: It reduces the performance of your code as a response per unit time, and it makes your code less readable.

You may also like: Exceptions in Java: You're (Probably) Doing It Wrong

Let's start by looking at how one can use an exception as the control flow by viewing the following example code. The business case for the code is:

    public static int findAge(String name) {
        try {
            String ageAsString = findUser(name);
            return ageAsString.length();
        } catch (NameNotFoundException e) {
            return 0;
        }
    }

    private static String findUser(String name) {
        if(name==null) {
            throw new NameNotFoundException();
        }
        return name;
    }


If the client provides a non-null name to the findAge method, it returns the length of the name, but if the user name is null, then the findUser method will throw a NameNotFoundException, and in that case, the findAge method  will return 0.

How can one refactor this code without an exception? Frankly, there multiple ways to do so, but only one of them is provided here.

    public static int findAgeNoEx(String name) {
        String ageAsString = findUserNoEx(name);
        return ageAsString.length();
    }

    private static String findUserNoEx(String name) {
        if(name==null) {
            return "";
        }
        return name;
    }


To find out the performance impact of exception, I prepared the following code which calls 10M times both implementation and it is seen that the exception costs thousands of milliseconds on my Intel Core i7-3630QM CPU.

public class ControlFlowWithExceptionOrNot {
    public static class NameNotFoundException extends RuntimeException {
        private static final long serialVersionUID = 3L;
    }

    private static final int TRIAL = 10000000;

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        for (int i = 0; i < TRIAL; i++) {
            findAgeNoEx(null);
        }
        System.out.println("Duration :" + (System.currentTimeMillis() - start));

        long start2 = System.currentTimeMillis();
        for (int i = 0; i < TRIAL; i++) {
            findAge(null);
        }
        System.out.println("Duration :" + (System.currentTimeMillis() - start2));
    };

    public static int findAge(String name) {
        try {
            String ageAsString = findUser(name);
            return ageAsString.length();
        } catch (NameNotFoundException e) {
            return 0;
        }
    }

    private static String findUser(String name) {
        if (name == null) {
            throw new NameNotFoundException();
        }
        return name;
    }

    public static int findAgeNoEx(String name) {
        String ageAsString = findUserNoEx(name);
        return ageAsString.length();
    }

    private static String findUserNoEx(String name) {
        if (name == null) {
            return "";
        }
        return name;
    }

}


Output:

Duration :16
Duration :6212


If we compare the two findAge methods in terms of readability, the one without exception is crystal clear, whatever the findUser method returns, take its length, and we are sure that the  findUser method will return a string. However, the one with the exception is a bit confusing: The return of the findUser method is not clear. It may return a string, or it may throw an exception, and from the method's signature, it is not visible. For this reason, the functional programming paradigm does not welcome exceptions. 

Finally, it would be better if you use an exception when there is a real exceptional case. If you use exceptions for the control flow, it may cause performance issues and your code may be less readable. 

Hope this helps! Please share your thoughts in the comments section.

Further Reading

Nine Best Practices to Handle Exceptions in Java

Exceptions in Java: You're (Probably) Doing It Wrong

Making an Exception-Handling Mechanism

Java (programming language) Flow (web browser)

Opinions expressed by DZone contributors are their own.

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Spring Transaction Propagation in a Nutshell
  • How to Convert XLS to XLSX in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!