-
Anomalies
can be roughly divided into two categories. Runtime exceptions and user-defined exceptions .
The first is the exception that the compiler cannot detect, such as out-of- bounds access to the array table below, division by 0, etc.
Generally visible: the
ArithmeticException、NullPointerException、ClassCastException、 NegativeArraySizeException、ArrayIndexOutOfBoundsException、StringIndexOutOfException
other is a custom "error" to meet the needs of users .
For example, a Student class limits the age range of students to [15,20]. When the member method setAge is called to set the age, if it does not meet the requirements (input errors are detected), a custom exception will be thrown. -
Exception handling
using try-catch statement
try {
int rst = 1/0;
System.out.println(rst);
} catch (ArithmeticException e) {
System.out.println("int类型下除法的分母不能为0!");
}
Rules and recommendations:
1. A catch statement can only catch one type of exception . If multiple exceptions may occur in the try code block, multiple catch statements are required.
2. In order to avoid unexpected program termination due to exceptions, the last catch statement parameter Use Exception e . Exception exception class is the parent class of all exception classes.
- Custom exception class
Definition: Create an exception subclass of the Exception class and add a construction method.
For example, define an illegal name exception class:
class IllegalNameException extends Exception
{
public IllegalNameException() {
super("姓名出错!");
}
public IllegalNameException(String s) {
super(s);
// 这里的s 是用户检测到错误后,抛出异常时候给出的信息
// 即 throw new IllegalNameException(s);
}
}
The combination of this exception class and method:
void setName(String s) throws IllegalNameException
{
if (s.length()<1 || s.length()>5) {
throw new IllegalNameException();
}
this.name = s;
}
Note: When a method declaration while unusual statement
The role of the exception:
try {
student.setName("Adam"); // student是一个Student类的实例
} catch (IllegalNameException e) {
System.out.println(e.getMessage()); // 通过getMessage方法的调用,得到自定义的错误信息
}
Thinking:
1. The introduction of exceptions increases the robustness and "fault tolerance" of the program . For programmers, they have more confidence in handling complex situations.
2. Combining the exception capture mechanism in C++ and comparing the exception capture mechanism in java, the difficulty that comes to mind: the nesting problem of try-catch and so on.
Example: Define a triangle class, including a construction method and a method for finding the area. The method for finding the area will throw a custom invalid triangle exception for data that cannot form a triangle.
class IllegalTriangleFormException extends Exception
{
public IllegalTriangleFormException() {
super("三边不能构成三角形");
}
public IllegalTriangleFormException(String s) {
super(s);
}
}
class Triangle
{
double a,b,c;
public Triangle(double d1,double d2,double d3) {
a=d1;
b=d2;
c=d3;
}
public double caluArea() throws IllegalTriangleFormException
{
if(a+b>c && a+c>b && c+b>a)
{
double p = (a+b+c)/2; // 半周长
double S = Math.sqrt(p*(p-a)*(p-b)*(p-c)); // 海伦公式
return S;
}else
{
throw new IllegalTriangleFormException();
}
}
}
public class ExpDemo04 {
public static void main(String[] args) {
Triangle triangle = new Triangle(3, 2, 5);
try {
System.out.println(triangle.caluArea());
} catch (IllegalTriangleFormException e) {
System.out.println(e.getMessage());
}
}
}