Manejo de Errores y Excepciones
Las excepciones permiten detectar y controlar errores durante la ejecución de un programa.
try y catch
try permite ejecutar código que puede generar errores y catch permite capturar las excepciones.
Ejemplo 1
try{
}catch(Exception e){
}
Ejemplo 2
catch(ArithmeticException e){
}
Ejercicios
- Crear un bloque try-catch.
- Manejar una división entre cero.
throw, throws y finally
throw y throws permiten generar excepciones y finally ejecuta instrucciones obligatorias.
Ejemplo 1
throw new Exception();
Ejemplo 2
finally{
}
Ejercicios
- Usar throw en un método.
- Agregar finally a un programa.