How to print the details of an exception:
Now if we want to print System error messages then
the catch block should be:
catch(ArithmeticException ae)
{
System.out.println(ae);
}
catch(NumberFormatException nfe)
{
nfe.printStackTrace();
}
catch(ArrayIndexOutOfBound ae )
{
System.out.println(ae.getMessage());
}
catch(Exception e)
{
System.out.println(e);
}
In java, we have 3 ways to find the details of Exception
which are unknown to the java program.
1) Using an object of exception
class.
2) Using public void printStackTrace().
3) Using public String
getMessage().
1)
Using an object of Exception
class:-
An object of Exception will print one name of
exception & nature of the message.
e.g.:
try
{
Int
x=Integer.ParseInt(“10x”);
}
catch(Exception e)
{
System.out.println(e);
}
//java.lang.NumberFormatException: For input String
“10x”
2)
Using public void printStackTrace():
(i) Whenever any exception takes
place in a java program, program execution will be terminated immediately and
JVM will push the details of the exception into stack memory.
(ii) Using printStactTrace()
method, we can print the name of the exception, nature of the message and line
number where the exception took place.
(iii) This method is actually
defined in java.lang.Throwable class and it is further inherited into
java.lang.Error and java.lang.Exception classes.
e.g.:
try
{
int x =
Integer.parseInt(“10x”);
}
catch(Exception e)
{
e.printStackTrace();
}
//java.lang.NumberFormatException :
for input string “10x” at line#.
Note: The above method should not be used as a part
of System.out.println();
3)
Using public String getMessage()
method:
(i) This is the method which is
defined in java.lang.Throwable class and it is inherited into java.lang.Error
and java.lang.Exception class.
(ii) Using this method, we can
print nature of the message only whose prototype is as follows-
public String getMessage();
e.g.:
try
{
int x=Integer.parseInt (“10x”);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
//For input String “10x”.
Example
to illustrate User defined exception:
NegativeAge.java
package nA; //step
1
public class NegativeAge extends
Exception //step 2 and step 3
{
public NegativeAge(String S); //step 4
{
super(S); //step
5
}
}
CheckAge.java
package check;
import nA.*;
public class CheckAge
{
void CheckAge(String s) throws NegativeAge, NumberFormatException
{
int age = Integer.parseInt(s);
if(age<0)
{
NegativeAge na = new NegativeAge(“Invalid
age.”);
throws na;
}
else
{
System.out.println(“Valid age.”)
}
}
}
//only compilation possible
because main is not used
UserDefinedExceptionDemo.java
Class UserDefinedExceptionDemo
{
public static void main(String args[])
{
try
{
String
age= args[0];
CheckAge
ca = new CheckAge();
ca.CheckAge(age);
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(“Please
pass one value.”);
}
catch(NumberFormatException nfe)
{
System.out.println(“Please
pass only integer value.”);
}
catch(NegativeAge na)
{
System.out.println(na);
}
}
}
No comments:
Post a Comment