Java Exceptions
Author
Mohibul Alam
Last Updated
6 years ago
License
Creative Commons CC BY 4.0
Abstract
this is an article about the examples of checked and unchecked exception.
this is an article about the examples of checked and unchecked exception.
\documentclass{article}
\usepackage[utf8]{inputenc}
%programming code package
\usepackage{listings}
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3
}
\title{Java Exception}
\author{bsse1004 }
\date{February 2019}
\begin{document}
\maketitle
\section{Example of Checked Exception}
\begin{itemize}
\item IOException : It is thrown when an input-output operation failed or interrupted .IOExceptions are thrown when there is any input / output file operation issues while application performing certain tasks accessing the files. IOException is a checked exception.
\begin{itemize}
\begin{lstlisting}
static ArrayList<BufferedImage> getFrames(File gif) throws IOException {
ArrayList<BufferedImage> frames = new ArrayList<BufferedImage>();
ImageReader ir = new GIFImageReader(new GIFImageReaderSpi());
ir.setInput(ImageIO.createImageInputStream(gif));
for (int i = 0; i < ir.getNumImages(true); i++) {
frames.add(ir.read(i));
}
// Release resources for Garbage Collection
ir.dispose();
return frames;
}
\end{lstlisting}
\end{itemize}
\item FileNotFoundException : This Exception is raised when a file is not accessible or does not open.
\begin{itemize}
\item example :
\begin{lstlisting}
//Java program to demonstrate FileNotFoundException
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
class File_notFound_Demo {
public static void main(String args[]) {
try {
// Following file does not exist
File file = new File("E://file.txt");
FileReader fr = new FileReader(file);
} catch (FileNotFoundException e) {
System.out.println("File does not exist");
}
}
}
\end{lstlisting}
\end{itemize}
\item NoSuchFieldException : It is thrown when a class does not contain the field (or variable) specified.
\item ClassNotFoundException :This Exception is raised when we try to access a class whose definition is not found
\item NoSearchMethodException : It is thrown when accessing a method which is not found.
\end{itemize}
\section{Example of Unchecked Exception}
\begin{itemize}
\item ArrayIndexOutOfBoundsException : It is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
\begin{itemize}
\item Example :
\begin{lstlisting}
// Java program to demonstrate ArrayIndexOutOfBoundException
class ArrayIndexOutOfBound_Demo
{
public static void main(String args[])
{
try{
int a[] = new int[5];
a[6] = 9; // accessing 7th element in an array of
// size 5
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println ("Array Index is Out Of Bounds");
}
}
}
\end{lstlisting}
\end{itemize}
\item Arithmetic Exception : It is thrown when an exceptional condition has occurred in an arithmetic operation.
\begin{itemize}
\item Example
\begin{lstlisting}
// Java program to demonstrate ArithmeticException
class ArithmeticException_Demo
{
public static void main(String args[])
{
try {
int a = 30, b = 0;
int c = a/b; // cannot divide by zero
System.out.println ("Result = " + c);
}
catch(ArithmeticException e) {
System.out.println ("Can't divide a number by 0");
}
}
}
\end{lstlisting}
\end{itemize}
\item IllegalArgumentException : IllegalArgumentException is intended to be used anytime a method is called with any argument(s) that is improper, for whatever reason.
\item StringIndexOutOfBoundsException : It is thrown by String class methods to indicate that an index is either negative than the size of the string
\begin{itemize}
\item Example:
\begin{lstlisting}
// Java program to demonstrate StringIndexOutOfBoundsException
class StringIndexOutOfBound_Demo
{
public static void main(String args[])
{
try {
String a = "This is like chipping "; // length is 22
char c = a.charAt(24); // accessing 25th element
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e) {
System.out.println("StringIndexOutOfBoundsException");
}
}
}
\end{lstlisting}
\end{itemize}
\item NullPointerException : This exception is raised when referring to the members of a null object. Null represents nothing
\begin{itemize}
\item Example
\begin{lstlisting}
//Java program to demonstrate NullPointerException
class NullPointer_Demo
{
public static void main(String args[])
{
try {
String a = null; //null value
System.out.println(a.charAt(0));
} catch(NullPointerException e) {
System.out.println("NullPointerException..");
}
}
}
\end{lstlisting}
\end{itemize}
\item NumberFormatException : This exception is raised when a method could not convert a string into a numeric format.
\begin{itemize}
\item Example
\begin{lstlisting}
// Java program to demonstrate NumberFormatException
class NumberFormat_Demo
{
public static void main(String args[])
{
try {
// "akki" is not a number
int num = Integer.parseInt ("akki") ;
System.out.println(num);
} catch(NumberFormatException e) {
System.out.println("Number format exception");
}
}
}
\end{lstlisting}
\end{itemize}
\item AssertionError : the AssertionError in Java is thrown when an assert statement fails (i.e. the result is false).
\item ExceptionInInitializerError :ExceptionInInitializerError, which is thrown when an error occurs within the static initializer of a class or object. Since an ExceptionInInitializerError isn’t ever the cause of a thrown error, catching such an exception provides an underlying causal exception that indicates what the actual source of the issue was.
\item StackOverflowError : The StackOverflowError in Java occurs when the application performs excessively deep recursion. However, what exactly qualifies as “excessively deep” depends on many factors.
\item NoClassDefFoundError : In most cases, a java.lang.NoClassDefFoundError is thrown when a class reference was available during compile time, but has since disappeared (for whatever reason) during execution.
\end{itemize}
\end{document}