Javatpoint Logo
Javatpoint Logo

ClosedChannelException in Java with Examples

When an I/O operation attempts to occur on a closed channel, or a channel that is closed to the intended operation, the class ClosedChannelException is triggered. That is, if this exception is thrown. It does not mean, however, that the channel is totally closed-just that the attempted operation is blocked.


The following is the hierarchy of ClosedChannelException:

ClosedChannelException in Java with Examples

Before moving on to the class's functions, let's have a look at the constructor details.

Constructor Description
ClosedChannelException() creates a class instance

Let's now talk about the methods that the Throwable class inherited. The following is a tabular representation of them:

Method Description
addSuppressed(Throwable exception) To enable the sending of this exception, add it to the exceptions that were suppressed.
fillInStackTrace() This Throwable object fills in the execution stack trace and stores information about the current status of the stack frame for the running thread.
getCause() Returns this Throwable's cause, or null if it has no known cause.
getLocalizedMessage() Provides a description of this Throwable that is localised. The description may be overridden by subclasses. The outcome will be the same as getMessage() if the subclass doesn't override this function.
getMessage() Return back this Throwable's complete message description.
getStackTrace() Provides back an array of stack trace elements, each of which stands for a single stack frame. provides access to the stack trace data that printStackTrace() prints.
getSuppressed() Returns an array with all the exceptions suppressed so that this exception could be raised.
initCause(Throwable cause) Sets the specified value as the Throwable's initial reason.
printStackTrace() This Throwable's backtrace is printed on the error output stream.
printStackTrace(PrintStream s) Prints this Throwable on the designated PrintStream along with its backtrace.
printStackTrace(PrintWriter s) Prints this Throwable to the designated PrintWriter along with its backtrace.
setStackTrace(StackTraceElement[] stackTrace) Sets this Throwable's stack trace items. It enables the client to alter the default stack trace and is intended for use with sophisticated systems and remote procedure call frameworks.
toString() provides a brief explanation of this Throwable in the following format: Name of this object's class: outcome of calling getLocalizedMessage() on the object. Only the class name is returned if getLocalizedMessage() returns null.

In this, all we will do is establish a channel, shut it down, and then attempt to read data from a closed channel. It will cause the ClosedChannelException to occur. The following are the steps:

Step -1: The RandomAccessFile class will be created in order to open a text file from your system in read-write mode, or "rw."

Step -2: Now, we use the FileChannel class to build a channel to the opened file.

Step -3: Next, using the ByteBuffer class, we establish a buffer to read bytes of data from this channel.

Step -4: Additionally, we define the encoding scheme as "US-ASCII" in the Charset class.

Step -5: Lastly, we shut the channel before beginning the process of reading this file.

As a result, a ClosedChannelException is raised whenever a read operation is tried on this channel. Here, we are simply printing a message. We catch the exception in the catch block, where you can add whatever Exception handling unique to your needs.




ClosedChannelException in Java with Examples

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA