/**
* <p>title: Catches exceptions and implements its own exception class </p>
* <p>description: Implements its own exception class by inheriting the exception class. and use Try-catch to catch this exception. </p>
* <p>copyright:copyright (c) 2003</p>
* <p>filename: </p>
* @version 1.0< br> */
Class MyException extends Exception {
Public myexception () {}
Public myexception (String msg) {
Super (MSG);
}
Public myexception (String msg, int x) {
Super (msg);
i = x;
}
Public int val () {return i;}
private int i;
}
public class Demoexception {
/**
*<br> Method Description: Use the default constructor in the MyException class
*<br> input parameters:
* <br> return Type:
*/
public static void A () throws MyException {
System.out.println (
) throwing MyEx Ception from A () ");
throw new MyException ();
}
/**
*<br> Method Description: Use constructor with information in MyException class
*<br> input parameters:
*<br> return type:
*/
Public static void B () throws MyException {
SystEm.out.println (
"Throwing MyException from B ()");
throw new MyException ("originated in B ()");
}
/**
*<br> method Description: Using the encoded constructor in MyException
*<br> input parameters:
*<br> return type:
*/
Public static void C () throws MyException {
System.out.println (
"throwing myexception from C ()");
throw new MyException (
originated in C ());
public static void Main (string[] args) {
try {
A ();
catch (MyException e) {
E.getmessage ();
try {
B ();
catch (MyException e) {
e.tostring ();
try {
C ();
catch (MyException e) {
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("Error code:" + E.val ());
}
}
}//end:)