[JAVA100 Example]009, exception capture and implementation of their own exception class

Source: Internet
Author: User
Tags constructor error code

/**
* <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:)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.