30, ఏప్రిల్ 2012, సోమవారం

DEFINING OUR OWN EXCEPTIONS IN JAVA



A ROUGH IMPLEMENTATION FOR THE THROWS CONSTRUCT
Step1:-Here we design the SecurityException  class by extending the Exception class
Step2:-Class{

Public void passwordCheck() throws SecurityException
{
Checking mechanism and throw class(our own class designed in the step1)
}
…………
}

Step3:- class with main method
{
Main()
{
Try
{
// call here the above throws method
}
Catch()
{
}
Finally{}
}

}
class own extends Exception
{
own(String msg)
{
super(msg);
}
}
class testexce
{
public static void main(String args[])
{
int marks=101;
try{
if(marks > 100)
throw new own("MARKS SHOULD NOT MORE THAN 100");
}
catch(own o)
{
System.out.println("EXCEPTION CAUGHT ");
//tracing by using the stack
o.printStackTrace();
System.out.println("after the printStackTrace method execuation ");
//here printing the error message directly sending the object
System.out.println(o);
System.out.println("THIS IS THE NORMAL EXECUATION AFTER THE EXCPETION");
System.out.println();
//inside the exception object calling the member to display the information
System.out.println(o.getMessage());
}
finally
{
System.out.println("THIS IS THE LASTSTATEMENT IN THE PROGRAM");
}
}
}
OUTPUT:-
E:\jit1>javac testexce.java
E:\jit1>java testexce
EXCEPTION CAUGHT
own: MARKS SHOULD NOT MORE THAN 100
        at testexce.main(testexce.java:18)
after the printStackTrace method execuation
own: MARKS SHOULD NOT MORE THAN 100
THIS IS THE NORMAL EXECUATION AFTER THE EXCPETION
MARKS SHOULD NOT MORE THAN 100
THIS IS THE LASTSTATEMENT IN THE PROGRAM
E:\jit1>

//DEMONSTRATION OF THROWS CONSTRUCT IN JAVA EXCEPTION HANDLING AN EXAMPLE WITH ALL MECHANISMS OF JAVA EXCEPTION HANDLING
//class-1
class IllegalMarkException extends Exception
{}
//class-2
class test
{
int marks;
test(int m)
{
marks=m;
}
void display() throws IllegalMarkException
{
if((marks<0) || (marks >100)) throw new IllegalMarkException();
System.out.println("The marks are"+marks);
}
}
//class-3
public class testimpl
{
public static void main(String args[])
{
test t=new test(-10);
try{
t.display();
}
catch(IllegalMarkException ime)
{
System.out.println("THE EXCEPTION WAS CAUGHT ");
ime.printStackTrace();
}
finally
{
System.out.println("THE LAST STATEMENT IN THE PROGRAM");
}
}
}
E:\jit1>javac testimpl.java
E:\jit1>java testimpl
THE EXCEPTION WAS CAUGHT
IllegalMarkException
        at test.display(testimpl.java:17)
        at testimpl.main(testimpl.java:32)
THE LAST STATEMENT IN THE PROGRAM

E:\jit1>

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి