Capture all violations

Source: Internet
Author: User
Tags getmessage throwable tostring

We can create a controller that captures all types of violations. The specific approach is to capture the underlying class violation type exception (there are other types of underlying violations, but exception is the basis for almost all programming activities). As follows:
catch (Exception e) {
System.out.println ("Caught an Exception");
}
This code captures any violation, so it is best to put it at the end of the list of controllers when it is actually used, to prevent any special unauthorized controller following it from failing.
for all offending classes commonly used by programmers, because the exception class is their basis, we don't get much information about the violation, but we can call methods from its underlying class Throwable:

String getMessage () The
gets a detailed message. The

String toString ()
Returns a brief description of the Throwable, including detailed messages, if any. The

Void Printstacktrace ()
void Printstacktrace (PrintStream)
prints out the call stack path for Throwable and Throwable. The call stack shows the order of method calls that take us to the location where the violation occurred. The first version of the
prints out the standard error and the second prints out our selection process. If you are working under Windows, you cannot redirect standard errors. Therefore, we are generally willing to use a second version and send the result to System.out, so that the output can be redirected to any path we wish.
In addition, we can also get some additional methods from the base class object of Throwable (the underlying type of all objects). One of the things that might be useful for an unauthorized control is getclass (), which is to return an object and use it to represent the class of the object. We can query the class name by GetName () or ToString () in turn. You can also perform some complex operations on class objects, although those operations are not necessary for unauthorized control. The class object is also described in detail later in this chapter.
The following is a special example that shows the use of the exception method (if you are having difficulty executing the program, refer to Chapter 3rd 3.1.2 subsection "assignment"):
 

: Exceptionmethods.java
//demonstrating the Exception Methods
package c09;

public class Exceptionmethods {public
  static void Main (string[] args) {
    try {
      throw new Exception ("Here's" my Exception ");
    } catch (Exception e) {
      System.out.println ("Caught Exception");
      System.out.println (
        "E.getmessage ():" + e.getmessage ());
      System.out.println (
        "e.tostring ():" + e.tostring ());
      System.out.println ("E.printstacktrace ():");
      E.printstacktrace ();}}
///:~


The program output is as follows:

Caught Exception
e.getmessage (): Here's my Exception
e.tostring (): Java.lang.Exception:Here's My exception< C23/>e.printstacktrace ():
java.lang.Exception:Here ' s my Exception at
        exceptionmethods.main


As you can see, this method provides a lot of information continuously--each type of information is a subset of the previous type of information.

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.