Java : Exception con ejemplos
Exception (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos Exception.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Code examples on this page uses the SampleException class below.
@SuppressWarnings("serial")
public class SampleException extends Exception {
public SampleException() {
}
public SampleException(String message) {
super(message);
}
public SampleException(String message, Throwable cause) {
super(message, cause);
}
public SampleException(Throwable cause) {
super(cause);
}
public SampleException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
public class Main {
public static void main(String[] args) {
try {
func1();
} catch (SampleException ex) {
final Exception e = ex;
e.printStackTrace();
// Result
// ↓
//SampleException
// at Main.func2 ...
// at Main.func1 ...
// at Main.main ...
// ...
}
}
private static void func1() throws SampleException {
func2();
}
private static void func2() throws SampleException {
throw new SampleException();
}
}
Constructors
Exception ()
final Exception e = new SampleException();
System.out.println(e); // SampleException
Exception (String message)
final Exception e = new SampleException("abcde");
System.out.println(e); // SampleException: abcde
System.out.println(e.getMessage()); // abcde
Exception (String message, Throwable cause)
final Exception cause = new SampleException("XYZ");
final Exception e = new SampleException("abcde", cause);
System.out.println(e); // SampleException: abcde
System.out.println(e.getMessage()); // abcde
System.out.println(e.getCause()); // SampleException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ
Exception (String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)
final Exception cause = new SampleException("XYZ");
final Exception e = new SampleException("abcde", cause, true, true);
System.out.println(e); // SampleException: abcde
System.out.println(e.getCause()); // SampleException: XYZ
e.addSuppressed(new SampleException("E1"));
e.addSuppressed(new SampleException("E2"));
// [SampleException: E1, SampleException: E2]
System.out.println(Arrays.toString(e.getSuppressed()));
System.out.println(e.getStackTrace().length > 0); // true
// enableSuppression = false
final Exception cause = new SampleException("XYZ");
final Exception e = new SampleException("abcde", cause, false, true);
System.out.println(e); // SampleException: abcde
System.out.println(e.getCause()); // SampleException: XYZ
e.addSuppressed(new SampleException("E1"));
e.addSuppressed(new SampleException("E2"));
System.out.println(Arrays.toString(e.getSuppressed())); // []
System.out.println(e.getStackTrace().length > 0); // true
// writableStackTrace = false
final Exception cause = new SampleException("XYZ");
final Exception e = new SampleException("abcde", cause, true, false);
System.out.println(e); // SampleException: abcde
System.out.println(e.getCause()); // SampleException: XYZ
e.addSuppressed(new SampleException("E1"));
e.addSuppressed(new SampleException("E2"));
// [SampleException: E1, SampleException: E2]
System.out.println(Arrays.toString(e.getSuppressed()));
System.out.println(e.getStackTrace().length); // 0
Exception (Throwable cause)
final Exception cause = new SampleException("XYZ");
final Exception e = new SampleException(cause);
System.out.println(e); // SampleException: SampleException: XYZ
System.out.println(e.getMessage()); // SampleException: XYZ
System.out.println(e.getCause()); // SampleException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Consulte el siguiente enlace.
Related posts
- Ejemplos de API
- Error
- Exception
- RuntimeException
- Throwable