Sobes.tech
Junior — Middle

Peut-on convertir une exception personnalisée, héritée de RuntimeException, en un type RuntimeException?

sobes.tech IA

Réponse de l'IA

Oui, une exception personnalisée héritée de RuntimeException est sa sous-classe et peut être convertie en RuntimeException sans problème.

Exemple :

class MyException extends RuntimeException {}

try {
    throw new MyException();
} catch (RuntimeException e) {
    // Ici e est un RuntimeException, bien qu'en réalité ce soit MyException
    System.out.println("Exception RuntimeException capturée");
}

Ainsi, l'héritage permet d'utiliser des exceptions personnalisées comme RuntimeException et de les convertir en ce type.