What Is Exception: Throwable
What Is Exception: Throwable
• What is Exception
Throwable
Exception Error
EXCEPTION HANDLING
• It is common to make mistakes while developing as well as
typing a program.
• A mistake might lead to an error causing the program to
produce unexpected results.
• Errors are the wrong that can make a program go wrong.
• An error may produce an incorrect output or may terminate
the execution of the program abruptly or even may cause
the system to crash.
• It is therefore important to detect and manage properly all
the possible error conditions in the program so that the
program will not terminate or crash during execution.
• There are two types of errors:
Compile-time errors
Run-time errors
Compile-time errors:
• The compile-time errors are detected and
displayed by the Java compiler, during the
compilation of the program.
Division by zero
Exceptions
• An exception is a run-time error.
• When the Java interpreter encounters an error such as dividing
an integer by zero, it creates an exception object and throws it
(informs that an error has occurred).
• If the exception object is not caught and handled properly, the
interpreter will display an error message and will terminate the
program.
• If one wants the program to continue with the execution of the
remaining code, then one should try to catch the exception object
thrown by the error condition and then display an appropriate
message for taking corrective action. The task is known as
exception handling.
• The purpose of exception handling mechanism is to provide a
means to detect and report an exceptional circumstance so that
appropriate action can be taken.
• Java exception handling is managed via five keywords: try,
catch, throw, throws and finally.
• The error handling code basically consists of two segments; one
to detect errors and to throw exceptions and the other to catch
exceptions and to take appropriate actions.
Java Exceptions: Some of the common exceptions are listed
in the following table:
Exception Meaning
ArithmeticException Arithmetic error, such as divide by zero.
ArrayIndexOutOfBoundsException Array index is out of bounds.
ArrayStoreException Assignment to an array element of an
incompatible type.
FileNotFoundException Attempt to access a nonexistent file.
IOException Caused by general I/O failures, such as
inability to read from a file.
NullPointerException Caused by referencing a null object
NumberFormatException Caused when a conversion between
strings and number fails.
OutOfMemoryException Caused when there is not enough
memory to allocate a new object
StringIndexOutOfBoundsException Caused when a program attempts to
access a nonexistent character
position in a string.
SerurityException Caused when an applet tries to perform an
action not allowed by the browser’s
security setting.
StackOverflowException Caused when the system runs out of the
stack space.
Syntax of Exception Handling Code: The basic concept of
exception handling is throwing an exception and catching it.
catch block
• A catch block defined by the keyword catch catches the exception thrown by the
try block and handles it appropriately.
}
count = count + 1 ;
}
System.out.println("\n Valid Numbers = " + count);
System.out.println("\n Invalid Numbers = " + invalid);
}
}
javac Number_Format_Exception.java
java Number_Format_Exception 10 10.75 50 C++ 50.5 15
Output:
Invalid Number: 10.75
Invalid Number: C++
Invalid Number: 50.5
Valid Numbers = 3
Invalid Numbers = 3
Multiple catch statements:
• It is also possible to have more than one catch statement
in the catch block.
• When an exception in a try block is generated, the Java
treats the multiple catch statements like cases in a switch
statement.
• The first statement whose parameter matches with the
exception object will be executed, and the remaining
statements will be skipped.
• Note that Java does not require any processing of the
exception at all. One can simply have a catch statement
with an empty block to avoid abortion, as shown below:
catch (Exception e) ;
• The catch statement simply ends with a semicolon, which
does nothing. This statement will catch an exception and
then ignore it.
Example:
// This program illustrates the use of the multiple catch statements
class Multiple_Catch
{
public static void main(String args[])
{
try
{
int a = args.length ;
System.out.println(" a = " + a);
int b = 42 / a ;
int c[ ] = {1};
c[42] = 99 ;
}
catch(ArithmeticException e)
{ System.out.println("\n Divide by zero " + e); }
catch(ArrayIndexOutOfBoundsException e)
{ System.out.println(" Array index out of bounds " + e);
}
System.out.println(" After the try/catch blocks");
}
}
Use of the finally statement:
• For example:
throw new ArithmeticException( );
throw new NumberFormatException( );
Example: Program to illustrate the use of throwing an exception
import java.lang.Exception ;
class MyException extends Exception
{
MyException(String message)
{ super(message) ; }
}
class User_Exception
{
public static void main(String args[])
{
int x = 5, y= 1000 ;
try
{
float z = (float)x / (float)y ;
if(z < 0.01)
{
throw new MyException(" Number is too small");
}
}
catch(MyException e)
{
System.out.println("\n Caught my exception");
System.out.println( e.getMessage( ) );
}
finally
{ System.out.println(" I am always here"); }
}
}
Cont.