Exception Classes in Java With Example

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

It is one of the popular built in exception classes in java.

The structure of the RuntimeException class is given as :


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

The specialty of Runtime Exception is that it unchecked in nature.So it is not necessary to check for null on every handle that is passed into a method.(We do not know or we do not want to know if the caller of the method has provided a valid handle). In java, part of the standard runtime checking that (java performs internally) if any call is made to a null handle,java automatically throw a NullPointerException. So we never need to write an Exception specification saying that a method may throw a NullPointerException.
As the compiler does not enforce exception specification for RuntimeException,Runtime Exceptions could propagate all the way to main() method without being caught.As a result printStackTrace() is called for that exception as the program exits.It is only possible not to catch an exception like this for RuntimeExceptions since all other handling is carefully enforced by the compiler. Runtime Exception represents either a programming error that we can not catch or one that we , as a programmer should have checked in our code.It means Runtime Exceptions represent a programming error. However this helps in debugging a different way.


public class DoNoCatch{
    static void foo()
    {
        throw new RuntimeException("generating in foo");
    }
    static void goo()
    {
        foo();
    }
     public static void main(String []args){
        goo();
     }
}
 

output of the code:
$javac DoNoCatch.java
$java -Xmx128M -Xms16M DoNoCatch
Exception in thread “main” java.lang.RuntimeException: generating in foo
at DoNoCatch.foo(DoNoCatch.java:4)
at DoNoCatch.goo(DoNoCatch.java:8)
at DoNoCatch.main(DoNoCatch.java:11)

Class UnknownHostException in Java

UnknownHostException is thrown from an application to indicate that the IP address of the host could not be determined like that host name does not matches with any of the known hosts.

A great example is shown on my socket page.

The class structure of UnknownHostException is written below:


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

SocketException in Java:

SocketException is thrown from the application to indicate that some exception happened while attempting to use a socket.

The class SocketException is given below:


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

WriteAbortedException in java:

An application throws WriteAbortedException in java when it tries reading a Stream of data that is incomplete as an exception was thrown while it was being written. The delete field holds the exception that terminated the output stream.The overridden getMessage() method provides the datailed exception message.

The structure of WriteAbortedException in java is given as:


public class java.io.WriteAbortedException extends
             java.io.ObjectStreamException{
//member element:
public Exception details;
//constructor
public WriteAbortedException(String msg,Exception e);
//methods
public String getMessage();
}
 

Here is the list of different exception classes in java,built in exception classes in java.

Class AWTException in AWT in Java:

An AWTException is thrown from an application when an exception has occurred in  Abstract Window Toolkit during execution.

The class structure of AWTException is given as:

public class java.awt.AWTException extends java.lang.Exception{
//constructor
public AWTException(String message);
//constructs an AWTException with the specified detailed message.
} 

Class ArithmeticException in Java:

ArithmeticException is thrown to indicate that an exceptional arithmetic condition has occurred.
Example divide by zero.

The class structure is as follows:

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

Class IndexOutOfboundException in Java:

IndexOutOfboundException object is thrown to indicate that an index (or some sort of ) is out of its allowed range or bounds. This is applicable to Array,Vector or a String.Application can subclass this class to indicate similar kind of exception.
Two subclasses of the  IndexOutOfboundException are as:

The structure of the class is as follows:

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

Ref- Oracle Exception classes

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