-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11Manejo de Errores en Dart.dart
More file actions
97 lines (83 loc) · 2.34 KB
/
Copy path11Manejo de Errores en Dart.dart
File metadata and controls
97 lines (83 loc) · 2.34 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
void main() {
// Ejemplo de Exception genérica
try {
throw Exception('Generic exception');
} on Exception catch (e) {
print('Error: $e'); // Output: Error: Exception: Generic exception
}
// Ejemplo de FormatException
try {
int.parse('NotANumber');
} on FormatException catch (e) {
print(
'Error: $e'); // Output: Error: FormatException: Invalid radix-10 number
}
// Ejemplo de ArgumentError
try {
checkPositive(-1);
} on ArgumentError catch (e) {
print(
'Error: $e'); // Output: Error: Invalid argument(s): The number must be positive
}
// Ejemplo de RangeError
try {
checkIndex(5, 3);
} on RangeError catch (e) {
print(
'Error: $e'); // Output: Error: RangeError (index): Invalid value: Not in range 0..2, inclusive: 5
}
// Ejemplo de StateError
try {
performOperation(false);
} on StateError catch (e) {
print('Error: $e'); // Output: Error: Bad state: Operation not allowed
}
// Ejemplo de UnimplementedError
try {
notImplementedFunction();
} on UnimplementedError catch (e) {
print(
'Error: $e'); // Output: Error: UnimplementedError: This function is not yet implemented
}
// Ejemplo de UnsupportedError
try {
unsupportedOperation();
} on UnsupportedError catch (e) {
print(
'Error: $e'); // Output: Error: Unsupported operation: This operation is not supported
}
// Ejemplo de excepción personalizada
try {
throw CustomException('This is a custom exception');
} on CustomException catch (e) {
print(
'Error: $e'); // Output: Error: CustomException: This is a custom exception
}
}
void checkPositive(int number) {
if (number < 0) {
throw ArgumentError('The number must be positive');
}
}
void checkIndex(int index, int length) {
if (index < 0 || index >= length) {
throw RangeError.index(index, length);
}
}
void performOperation(bool allowed) {
if (!allowed) {
throw StateError('Operation not allowed');
}
}
void notImplementedFunction() {
throw UnimplementedError('This function is not yet implemented');
}
void unsupportedOperation() {
throw UnsupportedError('This operation is not supported');
}
class CustomException implements Exception {
final String message;
CustomException(this.message);
@override
String toString() => 'CustomException: $message';
}