import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
/**
* {@link <a href=
* "https://[Log in to view URL]"
* target="_blank">Custom exception</a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
/**
* @see Trace
* @see Exception
*
*/
public static class CustomException extends Trace {
private static final long serialVersionUID = -69L;
private final Exception EX;
protected CustomException(Exception eX) {
EX = eX;
}
public String print() {
return this.print(EX);
}
}
public static abstract class Trace extends Throwable {
private static final long serialVersionUID = -69L;
/**
* @see Throwable
*
* {@link PrintWriter} {@link StringWriter}
*
*/
protected String print(Throwable ex) {
PrintWriter pw = null;
StringWriter sw = null;
try {
sw = new StringWriter();
pw = new PrintWriter(sw);
ex.printStackTrace(pw);
return sw.toString();
} finally {
closeWriter(pw);
closeWriter(sw);
}
}
/**
* @see Writer
*
*/
private void closeWriter(Writer writer) {
try {
if (writer != null)
writer.close();
} catch (IOException IOex) {
IOex.printStackTrace();
}
}
}
public static void exit() throws CustomException {
try {
int[] array = new int[4];
System.out.println(array[4]);
} catch (ArrayIndexOutOfBoundsException ex) {
throw new CustomException(ex);
}
}
public static void main(String args[]) throws Exception {
// Unit test - lancio di un'eccezione manuale
try {
exit();
} catch (CustomException ex) {
System.out.println(ex.print());
}
System.out.println("qui prosegue il flusso dopo un CustomException gestito");
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: