The error that occurs during execution which leads to
disturb in flow of execution is nothing but the exception .Exceptions can be of
any type and the ways to handle that exception are called as exception
handling.
Checked and
unchecked are the types of exceptions in java.
Checked
exceptions: a program has to
catch and handle that exception is called a checked exception .In the source
code only checked exceptions have to be declared.Checked exceptions are
declared by the compiler at compile time.
Unchecked
exception: unchecked
exceptions are not checked by the compiler. in the program unchecked exceptions
are not specified and it does not cause compilation error. The unchecked
exception handled at run time
some of the
exceptions in java are as follows.
NullPointerException:
If we are using any pointer to referring to the member
but that member is of null object then this exception thrown as Null means
nothing it means it represents nothing.NullPointerException it is an unchecked
exception.
ex-
class
NullPointer_code
{
public static
void main(String args[])
{
try {
String
a = null; System.out.println(a.charAt(0));
} catch(NullPointerException e) {
System.out.println("NullPointerException thrown..");
}
}
}
Output:
NullPointerException
thrown..
NumberFormatException:
Suppose we have created one class in which we are
assigning a string value to integer data type and if that method is not able to
convert that string into numeric format then This exception is
raised.NumberFormatException it is an unchecked exception.
class
NumberFormat_code
{
public static
void main(String args[])
{
try {
int num = Integer.parseInt ("abcd")
System.out.println(num);
} catch(NumberFormatException e) {
System.out.println("Number format exception thrown");
}
}
}
Output:
Number
format exception thrown
RuntimeException:
When any exception occurs during runtime then it is
represented by RuntimeException This represents any exception which occurs
during runtime.RuntimeException it is an unchecked exception.
ex-
public
class MyExceptionTest { public void testRuntimeException () { throw new
MyException();
}
new
MyExceptionTest().testRuntimeException();
}
}
}
class
MyException extends RuntimeException { public MyException() {
super();
}
}
Output:
MyException
StringIndexOutOfBoundsException:
It is thrown from String class methods to show an index
is either negative than the size of the string.StringIndexOutOfBoundsException
it is an unchecked exception.And it extends a Run
time exception.
NoSuchFieldException:
Suppose
if our class does not contain a field or variable and we are trying to call
that then it is thrown that exception.
NoSuchMethodException:
The exception is thrown when we access a method which is
not found. It means suppose we try to dynamically use a method on a class, and
the method does not actually exist then also it gives
NoSuchMethodException.NoSuchMethodException it is a checked exception.
Amarja Chede
K10
Nice concept 👌
ReplyDeleteGood
ReplyDeleteNice
ReplyDeleteInformative 👍
ReplyDeleteVery understanding
ReplyDeleteGreat work!!
ReplyDeleteGreat
ReplyDeleteGood work
ReplyDeleteAll concepts are explained very well and easily understood.
ReplyDeleteGood work 👍gay's,keep it up
ReplyDelete