Backend Development 3 min read

Why Swallowing Exceptions in Java Is Dangerous and How to Handle Them Properly

The article warns developers against swallowing exceptions in Java, explains the risks of empty catch blocks using a sample code snippet, and emphasizes the need for proper exception handling to avoid hidden errors and potential termination.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Why Swallowing Exceptions in Java Is Dangerous and How to Handle Them Properly

The author begins with a disclaimer that the article is not intended to encourage anyone to quit their job, especially given the current tough IT employment situation, but rather to share technical advice.

The main topic is exception handling in Java, highlighting that swallowing exceptions—i.e., leaving catch blocks empty—is a poor practice.

An example code snippet demonstrates a class SwallowException where the divide method may throw an ArithmeticException , and the main method catches Exception e but does nothing inside the catch block, then prints "OK".

The article explains that such empty catch blocks hide errors, making the program appear to run normally while an exception has actually occurred, which is dangerous.

It advises that developers should handle exceptions appropriately, such as logging the error or rethrowing it, rather than silently ignoring them.

The author invites readers to discuss other unusual exception handling cases and provides recommended reading links.

package com.sample.core.exception; public class SwallowException { public static void main(String[] args) { SwallowException swallowException = new SwallowException(); try { int a = 5; int b = 0; int result = swallowException.divide(a, b); System.out.println(result); } catch (Exception e) { // empty catch block } System.out.println("OK"); } public int divide(int a, int b) { int c = a / b; return c; } }

Javabackend developmentException Handlingcoding practicesSwallow Exception
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.