-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathExcepciones.java
More file actions
61 lines (46 loc) · 1.75 KB
/
Excepciones.java
File metadata and controls
61 lines (46 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class SaldosInsuficientesException extends Exception {
public SaldosInsuficientesException(String mensaje) {
super(mensaje);
}
}
class CuentaBancaria {
private double saldo;
public CuentaBancaria(double saldo) {
this.saldo = saldo;
}
public double obtenerSaldo() {
return saldo;
}
public void retiro(double monto) throws SaldosInsuficientesException {
if (saldo < monto) {
throw new SaldosInsuficientesException("Saldo insuficiente para retirar");
}
saldo -= monto;
}
}
public class Excepciones {
public static void main(String args[]) {
// Excepciones
// Manera de manejar situaciones inesperadas que ocurren durante
// la ejecución de un programa.
// Tipos de Excepciones en Java.
// Checked Exceptions - IOException o SQLException.
// Unchecked exceptions (Excepciones no verificadas).
// Durante la ejecución y no son verificadas en tiempo de compilación.
// NullPointerException o ArrayIndexOutOfBoundsException.
// Cuenta con 1000 pesos de saldo inicial.
CuentaBancaria cuenta = new CuentaBancaria(1000);
try {
System.out.println("Intentando retirar 200");
cuenta.retiro(200);
System.out.println("REtiro exitoso. Saldo restante: " + cuenta.obtenerSaldo());
System.out.println("Intentando retirar 300");
cuenta.retiro(300);
System.out.println("Que saldo tengo " + cuenta.obtenerSaldo());
System.out.println("Intentando retirar 600");
cuenta.retiro(600);
} catch (SaldosInsuficientesException e) {
System.out.println("Error: " + e.getMessage());
}
}
}