Throwable class setStackTrace () método : Aquí, vamos a aprender sobre el método setStackTrace () de la clase Throwable Class con su sintaxis y su ejemplo.
Throwable Class setStackTrace () método
- setStackTrace () Método está disponible en java.lang Class.
- setStackTrace () Método se utiliza para sistemas de pila elementos traza que se recuperarán mediante el uso de método getStackTrace ().
- setStackTrace () Método es un método no package, es accesible sólo con el objeto throw y si tratamos de acceder al método con el nombre static entonces obtendrá un error.
- setStackTrace () Método puede class una excepción en el momento de fijar los elementos traza de la pila.
NullPointerException- – Esta excepción puede class cuando el argumento dado es throw o cuando alguno de sus elementos existentes del seguimiento de la pila es throw.
-
public Throwable initCause(Throwable caus);
Sintaxis:
Parámetro (s):
StackTraceElement [] st_tr – representa un conjunto de “StackTraceElement”.
throw valor:
- El tipo class del método es
- null , no devuelve nada.
Ejemplo:
// Java program to demonstrate the example
// of Throwable initCause(Throwable caus)
// method of Throwable
public class InitCause {
public static void main(String args[]) throws Exception {
try {
// calling div() method
div(100, 0);
} catch (ArithmeticException ex) {
// Display the exception cause why it is
//thrown
System.out.println("Exception Cause:" + ex.getCause());
}
}
// This method divide two numbers and will
// throw an exception
public static void div(int d1, int d2) throws Exception {
try {
int res = d1 / d2;
} catch (ArithmeticException ex) {
// creating an exception
ArithmeticException ae = new ArithmeticException();
// instantiate exception cause by using
//initCause() method
ae.initCause(ex);
// throw an exception with its cause
throw (ae);
}
}
}
salida
Exception Cause:java.lang.ArithmeticException: / by zero