
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can we have an empty catch block in Java?
Yes, we can have an empty catch block. But this is a bad practice to implement in Java.
Generally, the try block has the code which is capable of producing exceptions, if anything wrong in the try block, for instance, divide by zero, file not found, etc. It will generate an exception that is caught by the catch block. The catch block catches and handles the exception. If the catch block is empty then we will have no idea what went wrong within our code.
Example
public class EmptyCatchBlockTest { public static void main(String[] args) { try { int a = 4, b = 0; int c = a/b; } catch(ArithmeticException ae) { // An empty catch block } } }
In the above code, the catch block catches the exception but doesn’t print anything in the console. This makes the user think that there is no exception in the code. So it's a good practice to print corresponding exception messages in the catch block.
Output
// An empty catch block
- Related Articles
- Can we have a try block without a catch block in Java?\n
- Can we declare a try catch block within another try catch block in Java?
- Can we to override a catch block in java?
- Can a try block have multiple catch blocks in Java?
- Can we define a try block with multiple catch blocks in Java?
- Can finally block be used without catch in Java?
- Why we can't initialize static final variable in try/catch block in java?
- Can we write code in try/catch block in JSP as well?
- Can we have a return statement in the catch or, finally blocks in Java?
- What is the catch block in Java?
- Can we throw an Unchecked Exception from a static block in java?
- Is it possible to catch multiple Java exceptions in single catch block?
- Is it possible to have multiple try blocks with only one catch block in java?
- Can we have variables and methods in an enum in Java?
- Can we have integers as elements of an enum in Java?

Advertisements