6) Best Practices

package com.gs.ilp.exceptionhandling;

public class MyException extends Exception {

	private String errorCode;

	MyException(String message, Exception e, String errorCode) {
		super(message, e);
		this.setErrorCode(errorCode);
	}

	public MyException(String string, String string2) {
		super(string);
		errorCode = string2;
	}

	public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

}
package com.gs.ilp.exceptionhandling;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.SQLException;

public class TestMyOwnExp {
	public static void main(String[] args) {

		try {
			try {
				m1();
			} catch (ArrayIndexOutOfBoundsException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (MyException e) {
			e.printStackTrace();
		}

	}

	private static void m1() throws MyException, ArrayIndexOutOfBoundsException, SQLException {
		m2();

	}

	private static void m2() throws MyException, ArrayIndexOutOfBoundsException, SQLException {

		try {
			FileInputStream fileInputStream = new FileInputStream("vfdg");
			Connection con = null;
			con.createStatement();

		} catch (FileNotFoundException e) {
			throw new MyException("my file found", e, "34");
			//throw new MyException("my file found", "34");
		} catch (ArrayIndexOutOfBoundsException eq) {

		}

	}
}

Last updated