Java Assignment
Java Assignment
EXCEPTION
.Exceptional event.
. Error that occurs during runtime.
. Examples-
“If the exception object is not handled properly, it will display the
error and will terminate the program”.
. EXCEPTION TYPES
. Exception are of two types-
-Built-in Exceptions
1) Arithmetic Exception
It is thrown when an exceptional condition has occurred in an
arithmetic operation.
2) ARRAYINDEXOUTOFBOUNDEXCEPTION
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 size of the array.
3) Class Not Found Exception
This Exception is raised when we try to access a class whose
definition is not found.
4) File Not Found Exception
This Exception is raised when a file is not accessible or does not
open.
5) IO Exception
It is thrown when an input – output operation failed or
interrupted.
6) Interrupted Exception
It is thrown when a thread is waiting, sleeping, or doing some
processing, and it is interrupted.
7) Null Pointer Exception
This exception is raised when referring to the members of a null
object. Null represents nothing.
Example-
// Java program to demonstrate Arithmetic exception
Class Arithmetic Exception _ 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(“ Cant’t divide a number
by 0”);
.EXCEPTION BLOCK
There are two Block of exception in Java -
they are
1) Try Block- It is used to enclose the code that might
throw an exception. It must be used within the
method.
If an exception occurs at the particular statement of
try block, the rest of the block code will not be
executed. So, it is recommended not to keeping the
code in try block that will not throw an exception.
Java try must be followed by either catch or finally
block.
Syntax of Java try-catch
Try {
//code that may throw an exception
} catch(Exception_class_Name ref){ }
2) Catch Block- It is used to handle the exception by
declaring the type of exception within the
parameter. The declared exception must be the
parent class exception ( Exception) or the
generated exception type. However, the good
approach is to declare the generated type of
exception.
The catch block must be used after the try block
only. You can use multiple catch block with a single
try block
CHECKED EXCEPTIONS
IOEXCEPTION
. Working with the file system or data streams using java.io package.
INTERRRUPTED EXCEPTION
Whenever a java thread calls join(),sleep(), or wait() it goes into
either the WAITING state or the TIMED_WAITING state.
UNCHECKED EXCEPTIONS
For unchecked exceptions, the compiler doesn’t check during the
compilation process. Hence, it isn’t mandatory for the method to
handle these exceptions.
There are different scene where illegal uses if null causes Null pointer
exception. Let’s consider some of them.
This exception indicates that the index is either greater than or equal
to the size of the string. String index out of Bounds exception extends
index out of bound exception.