Create your own violation

Source: Internet
Author: User

Does not necessarily have to use Java violations. This must be mastered, as it is often necessary to create your own violations to point out a particular error that your library might generate-but this error is unpredictable when you create a Java hierarchy.
To create your own offending class, you must inherit from an existing type of violation-preferably in the sense of the new violation. Inheriting a violation is fairly straightforward:

: Inheriting.java
//Inheriting your own exceptions

class MyException extends Exception {
  public MyException () {} public
  myexception (String msg) {
    super (msg);
  }
}

public class Inheriting {public
  static void F () throws MyException {
    System.out.println (
      "throwing MyException from F () ");
    throw new MyException ();
  }
  public static void G () throws MyException {
    System.out.println (
      "throwing myexception from G ()");
    throw new MyException ("originated in G ()");
  }
  public static void Main (string[] args) {
    try {
      f ();
    } catch (MyException e) {
      e.printstacktrace (); c25/>}
    try {
      g ();
    } catch (MyException e) {
      e.printstacktrace ();
    }
}}///:~


Inheritance occurs when a new class is created:

Class MyException extends Exception {public
  myexception () {} public
  myexception (String msg) {
    super (msg); c5/>}
}


The key here is "extends Exception", meaning that there is more to it than the full meaning of a Exception. The increase in the number of code is very small--only two builders were actually added to define how the myexception was created. Keep in mind that if we don't explicitly call an underlying class builder, the compiler automatically invokes the underlying class default builder. In the second builder, the base class Builder with a string parameter is explicitly called by using the Super keyword.
The results of the program output are as follows:

Throwing MyException from F ()
myexception at
        inheriting.f (inheriting.java:16) at
        Inheriting.main ( inheriting.java:24)
throwing myexception from G ()
myexception:originated into G () at
        INHERITING.G ( INHERITING.JAVA:20) at
        Inheriting.main (inheriting.java:29)


As you can see, there is a lack of detailed information in the myexception violation from the F () "throw".
When you create your own violations, you can also take more action. We can add additional builders and members:

: Inheriting2.java//Inheriting your own exceptions class MyException2 extends Exception {public MyException2 () {}
  Public MyException2 (String msg) {super (MSG);
    Public MyException2 (String msg, int x) {super (MSG);
  i = x;
  public int val () {return i;}
private int i; public class Inheriting2 {public static void F () throws MyException2 {System.out.println ("throwing myexc
    Eption2 from F () ");
  throw new MyException2 ();
    public static void G () throws MyException2 {System.out.println ("throwing MyException2 from G ()");
  throw new MyException2 ("originated in G ()");
    public static void H () throws MyException2 {System.out.println ("throwing MyException2 from H ()");
  throw new MyException2 ("originated in H ()", 47);
    public static void Main (string[] args) {try {f ();
    catch (MyException2 e) {e.printstacktrace ();
    try {g ();
  catch (MyException2 e) {    E.printstacktrace ();
    try {h ();
      catch (MyException2 e) {e.printstacktrace ();
    System.out.println ("e.val () =" + E.val ()); }
  }
} ///:~

A data member I was added at the time, a special method was added to read the value, and an extra builder was added to set that value. The output results are as follows:

Throwing MyException2 from F ()
MyException2 at
        inheriting2.f (inheriting2.java:22) at
        Inheriting2.main ( inheriting2.java:34)
throwing MyException2 from G ()
myexception2:originated into G () at
        inheriting2.g ( inheriting2.java:26) at
        inheriting2.main (inheriting2.java:39)
throwing MyException2 from H ()
Myexception2:originated in H () at the
        Inheriting2.h (inheriting2.java:30) at
        Inheriting2.main ( inheriting2.java:44)
e.val () = 47

Since the offence is only another form of object, the process can be continued to further enhance the ability of the offending class. But be aware that for client programmers who use their own packages, they may miss all of these enhancements. Because they may simply be looking for a generated violation, and nothing else to do-this is the standard usage of most Java library breaches. If this happens, it is possible to create a new type of violation, which contains almost no code:
: Simpleexception.java
Class Simpleexception extends Exception {
} ///:~
It relies on the compiler to create the default builder (automatically invokes the default builder for the underlying class). Of course, in this case, we don't get a simpleexception (String) builder, but it doesn't actually work very often.

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.