Custom Java exception

Source: Internet
Author: User

1. Preface:

Your program will crash one day. When it crashes, we need to know where it is, why it crashes, how data is stored or lost, and so on. We can design our own Java exceptions by inheriting the subclass of Java. Lang. throwable: exception. The exception class is used to describe exceptions that can be captured by a program, such as classnotfoundexception. Note that custom exception classes can also have an inheritance relationship. You also need to design a constructor for custom exception classes to facilitate the construction of custom exception objects.

2. design instance analysis:
This is a relatively complete design of custom exception classes, which is actually relatively templated.

Package playground;
Import java. Io .*;

Public class myexception extends exception {
Private int ID; // a unique ID
Private string classname; // The name of the class
Private string method; // The Name Of The Method
Private string message; // a detailed message
Private myexception previous =
NULL; // the exception which was caught
Private string separator = "/N"; // line Separator

Public myexception (int id, string classname, string method,
String message, myexception previous ){
This. ID = ID;
This. classname = classname;
This. method = method;
This. Message = message;
This. Previous = previous;
}

Public String traceback (){
Return traceback ("/N ");
}

Public String traceback (string SEP ){
This. Separator = Sep;
Int level = 0;
Myexception E = this;
String text = line ("calling sequence (top to bottom )");
While (E! = NULL ){
Level ++;
Text + = line ("-- level" + level + "--------------------------------------");
Text + = line ("Class/method:" + E. classname + "/" + E. method );
Text + = line ("ID:" + E. ID );
Text + = line ("message:" + E. Message );
E = E. Previous;
}
Return text;
}

Private string line (string s ){
Return S + separator;
}

}

Let's take a look at these things one by one:
In this custom exception class that inherits the exception class, we define the following variables:
ID: Independent identifier, which is used to identify where an error occurs in the class and capture it.
Classname: captures the name of the wrong class.
Method: The name of the method to capture this error.
Message: describes the entire event.
Previous: it is an instance of myexception. If it is the first instance in the linked list, it is null.

Let's see what methods are defined:
Traceback (): generate a trace of the data stored in the exception class. Use newline as the Separator
Traceback (string SEP): it is the same as the previous one. You can define the separators by yourself.
Line (string S): a method used by traceback.

Step 1: First throw the first exception:
There are two cases:
A. If the Program actively throws an exception somewhere, you will throw an error, for example:
Throw new myexception (1, "person", "getsalary ",
"Trying to get a salary, but no person was specified", null );

B. This exception is thrown when another exception occurs, for example:
Catch (exception e ){
Throw new myexception (4, "person", "read", E. tostring (), null );
}

If a method throws a myexception, the method is defined as follows:
Public void read () throws myexception...

Step 2: Next we will throw an exception, for example:
Person P = new person ();
P. setpersonid (ID );
Try {
P. Read ();
S + = P. getsalary ();
}
Catch (myexception e ){
Throw new myexception (1, "stats", "getallsalaries ",
"Cocould not get salary for" + id, e );
}

Our policy is as follows: place the methods we may throw exceptions under our monitoring (try {}), and then something happens to us in the processing area (catch {}) to handle this exception, you can handle it on the spot, or you cannot handle it in other places (throw ()). Note that we can see the last parameter E. We need to add this exception to the linked list of the exception class in each new exception.

Step 3: Tracing exceptions
The exception chain we designed above is as follows:
Calling sequence (top to bottom)
-- Level 1 --------------------------------------
Class/method: salaryservlet/doget
ID: 7
Message: trying to get the total salary for employees
-- Level 2 --------------------------------------
Class/method: stats/getallsalaries
ID: 1
Message: cocould not get salary for Lincoln
-- Level 3 --------------------------------------
Class/method: person/read
ID: 3
Message: Java. SQL. sqlexception: [Hansen] Invalid object name 'xemployee '.
-- Level 4 --------------------------------------
Class/method: person/read
ID: 999
Message: sqlexception. State/error code: s0002/208

In this scenario, a servlet is triggered. It calls getallsalaries and then the read method. In the read method, an sqlexception occurs.

3. Summary:
The custom exception class gives us the exception we like, and nothing else.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/gnuhpc/archive/2009/09/12/4545979.aspx

Related Article

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.