Exception Classes in Java With Example

0
586
Spread the love
  • 67
  • 99
  • 55
  • 45
  •  
  •  
  •  
  • 18
  •  
  •  
    284
    Shares

The methods(wait(),join()) are designed to throw an InterruptedException. These methods temporarily suspend the execution of a thread.If a Thread is waiting for one of these methods to return and another thread calls interrup() on the waiting thread,the method that is waiting throws an InterruptedException.

It is also common exception classes in java.

The structure of the class InterruptedException is given as :

public class java.lang.InterruptedException extends java.lang.Exception{
//constructor
public InterruptedException();
//constructs an empty InterruptedException object that is an object with no message specified.
public InterruptedException(String message);
//constructs an InterruptedException object with the message specified.
} 

InstantiationException in Java:

InstantiationException is very common built in exception classes in java and is thrown from the application when the application tries to create an instance of a class using the newInstance() method defined in class Class,but the specified class object can not be instantiated because it is an interface or an abstract class.

The structure of the InstantiationException class is given below:

public class java.lang.InstantiationException extends java.lang.Exception{
//constructor
public InstantiationException();
//constructs an empty InstantiationException object that is an object with no
// message specified.
public InstantiationException(String message);
//constructs an InstantiationException object with the message specified.
}

Class UFTDataFormatException in Java

UFTDataFormatException class signals that a malformed UFT-8 String has been read in a data input stream or by any class that implements the DataInput interface.

The structure of the class UFTDataFormatException is given as:

public class java.io.UFTDataFormatException extends java.io.IOException{
//constructors
public UFTDataFormatException();
//constructs an empty UFTDataFormatException object that is an object with no message specified.
public UFTDataFormatException(String message);
//constructs an UFTDataFormatException object with the message specified.
}

Class NotSerializableException in Java:

An application that throws NotSerializableException indicates that the target object , that it tried to serialize,could not be serialized.This can happen to classes which does not implement Serializable interface. A class may implement Serializable interface but may refer or contain a non serializable object,in this case when we try to serialize the class,we will get NotSerializableException.

A subclass of a serializable class can prevent itself from being serialized by throwing this (NotSerializableException) exception from its writeObject() or readObject() methods.

It is a child class of ObjectSreamException and is common exception classes in java.

The structure of the NotSerializableException class given as :

public class java.io.NotSerializableException extends java.io.ObjectStreamException{
//constructors:
public NotSerializableException();//default constructor
public NotSerializableException(String className);
} 

Class NoRouteToHostException in Java:

An application can throw this -NoRouteToHostException exception to indicate a socket could not be connected with remote host, as the host could not be connected due to one of the following reasons:

  • Host could not be connected before establishing socket
  • Some intermediate link/links between local machine to host is/are down.
  • Host may be behind a firewall.

This excaption-NoRouteToHostException is a subclass of SocketException and is common exception classes in java.

The structure of NoRouteToHostException is given as:

public class java.net.NoRouteToHostException extends java.io.SocketException{
//constrcutors:
public NoRouteToHostException();//default constrcutor
public NoRouteToHostException(String message);
} 

Class NotActiveException in Java:

An application can throw NotActiveException if it invokes a method either the method never invoked in right time or the method is invoked at a wrong context.
If an ObjectInputStream or ObjectOutputStream is not currently active,the operations on one of these could not be performed.
NotActiveException is a subclass of ObjectStreamException and is common exception classes in java.

The structure of NotActiveException class is given as:

public class java.io.NotActiveException extends java.io.ObjectStreamException{
//constructors:
public NotActiveException();//default constructor
public NotActiveException(String message);
} 

IllegalStateException in Java

IllegalStateException is common exception classes in java and is thrown from code to indicate that a method has been invoked on an object that is not in an appropriate state to be able to perform the specified operation.

The structure of the class IllegalStateException in Java is given as:

public class java.lang.IllegalStateException extends java.lang.RuntimeException{
//constructor
public IllegalStateException();
//constructs an empty IllegalStateException object that is an object with no message specified.
public IllegalStateException(String message);
//constructs an IllegalStateException object with the message specified.
} 

Class BindException In Java:

An application can throw this -BindException exception to indicate a socket could not be bound to the local address and port.It is very common exception classes in java.

The structure of BindException is given as:

public class java.net.BindException extends java.io.SocketException{
//constrcutors:
public BindException();//default constrcutor
public BindException(String message);
} 

Class ProtocolException in Java:

ProtocolException is common exception classes in java and is thrown from an application to indicate that there is an error in the underlying protocol like TCP Error.
It is thrown from Socket class.

The class ProtocolException is defined as :

public class java.io.ProtocolException extends java.lang.IOException{
//constructor
public ProtocolException();
//constructs an empty ProtocolException object that is an object with no message specified.
public ProtocolException(String message);
//constructs an ProtocolException object with the message specified.
}

Class MalformedURLException in Java

MalformedURLException is common exception classes in java when working with network and is thrown from an application to indicate that a Malformed url has occurred.There could be three scenarios:

  1. No legal  protocol could be found in the specification String
  2. The string could not be parsed.
  3. The URL was not built correctly.

One of the above causes the MalformedURLException .

The class MalformedURLException  is defined as below:

public class java.io.MalformedURLException extends java.lang.IOException{
//constructor
public MalformedURLException();
//constructs an empty MalformedURLException object that is an object with no message specified.
public MalformedURLException(String message);
//constructs an MalformedURLException object with the message specified.
}

Class InterruptedIOException in Java

InterruptedIOException is another popular built in exception classes in java and is thrown from an application to indicate that an input output operation has been interrupted.

The structure of the class InterruptedIOException is given below:

public class java.io.InterruptedIOException extends java.io.IOException{
//Member elements:
public int bytesTransferred;
//tells us how many bytes has been transferred as part of the IO-operation before it was interrupted.
//constructor
public InterruptedIOException();
//constructs an empty InterruptedIOException object that is an object with no message specified.
public InterruptedIOException(String message);
//constructs an InterruptedIOException object with the message specified.
}

Class FileNotFoundException in Java

FileNotFoundException is thrown from an application to indicate that a file specified to perform some operation could not be found.It is caused by an attempt to access a non existent file.It is also a very common built in exception classes in java.

The structure of the class FileNotFoundException is given as:

public class java.io.FileNotFoundException extends java.io.IOException{
//constructor
public FileNotFoundException();
//constructs an empty FileNotFoundException object that is an object with no message specified.
public FileNotFoundException(String message);
//constructs an FileNotFoundException object with the message specified.
}

Class IOException in Java

IOException is throen from an application to indicate that an IOException or some sort of Exception has occurred. It also covers the general IO failures and inability to read from a file.

The structure of the class IOException  is given as:

public class java.io.IOException extends java.lang.Exception{
//constructor
public IOException();
//constructs an empty IOException object that is an object with no message specified.
public IOException(String message);
//constructs an IOException object with the message specified.
}

Class EOFException in Java

EOFException is popular built in exception classes in java and is thrown from an application indicating that an end of file has been reached unexpectedly during input.This exception is mainly used by DataInputStream classe. This class expects a binary file in a specified format and for which the end of stream is an unusual condition.Most other input Streams return a special value on end of Streams.

The structure of the class EOFException is given as:


public class java.io.EOFException extends java.io.IOException{
//constructor
public EOFException();
//constructs an empty EOFException object that is an object with no message specified.
public EOFException(String message);
//constructs an EOFException object with the message specified.
}

Class UnknownServiceException in Java

UnknownServiceException is also common exception classes in java and is thrown from the application to indicate that an unknown service exception has occurred.when we try to get/call a service and the service we requested of a perticular network connection does not exist. This is could be due to MIME type(Multipurpose Main Extension) returned by an URL connection does not valid or the application is attempting to write to a read only URL connection.

The structure of the class UnknownServiceException is defined as :


public class java.io.UnknownServiceException extends java.lang.IOException{
//constructor
public UnknownServiceException();
//constructs an empty UnknownServiceException object that is an object with no message specified.
public UnknownServiceException(String message);
//constructs an UnknownServiceException object with the message specified.
}

NumberFormatException in java

NumberFormatException is common exception classes in java and is thrown from an application to indicate that the application has attempted to convert a String to one of the numeric types but the String does not have appropriate format. so this exception occurs when we try to convert an invalid String to number or vice versa. This is mostly thrown by classes of System package.
It is a child class of IllegalArgumentException which is again a child class of RuntimeException.

The Structure of the NumberFormatException class is given below:

public class java.lang.NumberFormatException extends java.lang.IllegalArgumentException{
//constructor
public NumberFormatException();
//constructs an empty NumberFormatException object that is an object with no message specified.
public NumberFormatException(String message);
//constructs an NumberFormatException object with the message specified.
}

RuntimeException in Java:

RuntimeException is the super class of all Exceptions which can be thrown during the normal operation of the java virtual machine.A method is required to declare in its throws clause.Any subclasses of the RuntimeException that might be thrown during the execution of the method but not caught.
In  simple words it is the base class for all exceptions that indicate some kind of programming issue or a bug. They originated as our or client programming using our library improperly. Because the indicate bugs,we virtually never catch a RuntimeException. It occurs during run time and automatically.In our own package we may throw some of the RuntimeExceptions.
For this exception, we can not directly use it, but it has lots of subclasses which can not be declared in the throws clause of a method definition.(They are declared as they are runtime conditions)

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 67
  • 99
  • 55
  • 45
  •  
  •  
  •  
  • 18
  •  
  •  
    284
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here