Java's Exception chaining

What is abnormal chain?

We often think: To catch an exception after another exception is thrown, and you want to preserve the original exception information, which is called the exception chain.

What way?

In JDK1.4 before, as a programmer, we have to write your own code to save raw exceptions.

Now, in all subclasses of Throwable constructor can accept a cause (cause) object as a parameter.

This cause is used to indicate the original exception, so by the original exception is passed to the new exception, even if the current location to create and throw a new exception, but also through the exception chain to track where the exception occurred first.

We use the following code to demonstrate for everyone:

//异常链
class DynamicFieldException extends Exception{}
public class DynamicFields {
	private Object[][] fields;
	public DynamicFields(int initialSize) {
		fields = new Object[initialSize][2];
		for(int i=0;i<initialSize;i++) 
			fields[i] = new Object[] {null,null};
	}
	public String toString() {
		StringBuilder result = new StringBuilder();
		for(Object[] obj:fields) {
			result.append(obj[0]);
			result.append(": ");
			result.append(obj[1]);
			result.append("\n");
		}
		return result.toString();
	}
	private int hasField(String id) {
		for(int i = 0;i<fields.length;i++)
			if(id.equals(fields[i][0]))
				return i;
		return -1;
	}
	private int 
	getFieldNumber(String id) throws NoSuchFieldException{
		int fieldNum = hasField(id);
		if(fieldNum == -1)
			throw new NoSuchFieldException();
		return fieldNum;
	}
	private int makeField(String id) {
		for(int i = 0;i<fields.length; i++)
			if(fields[i][0] == null) {
				fields[i][0] = id;
				return i;
			}
		Object[][] tmp = new Object[fields.length+1][2];
		for(int i = 0;i<fields.length;i++)
			tmp[i]=fields[i];
		for(int i = fields.length;i<tmp.length;i++)
			tmp[i] = new Object[] {
					null,null
			};
		fields = tmp;
		// Recursive call with expanded fields:
		return makeField(id);
	}
	public Object
	getField(String id) throws NoSuchFieldException{
		return fields[getFieldNumber(id)][1];
	}
	public Object setField(String id,Object value)
	throws DynamicFieldException{
		if(value == null) {
			DynamicFieldException dfe = new DynamicFieldException();
			dfe.initCause(new NullPointerException());
			throw dfe;
		}
		int fieldNumber = hasField(id);
		if(fieldNumber == -1)
			fieldNumber = makeField(id);
		Object result = null;
		try {
			result = getField(id);
		}catch(NoSuchFieldException e) {
			throw new RuntimeException(e);
		}
		fields[fieldNumber][1] = value;
		return result;
	}
	public static void main(String[] args) {
		DynamicFields df = new DynamicFields(3);
		System.out.println(df);
		try {
			df.setField("d", "A value for d");
			df.setField("Number",47);
			df.setField("Number2",48);
			System.out.println(df);
			df.setField("d", "A new value for d");
			df.setField("Number3", 11);
			System.out.println("df:"+df);
			System.out.println("df.getField(\"d\"):"+df.getField("d"));
			Object field = df.setField("d", null);
		}catch(NoSuchFieldException e) {
			e.printStackTrace(System.out);
		}catch(DynamicFieldException e) {
			e.printStackTrace(System.out);
		}
	}
}

 

The results demonstrate:

 

Our own to read the code, more conducive to our own ability to exercise!

We hope to be able to help, thank you!

Guess you like

Origin blog.csdn.net/qq_41026809/article/details/92002635