This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
MetodoCritico(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Error en MetodoCritico: {ex.Message}"); | |
throw; // Relanza la excepci贸n despu茅s de registrar detalles | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
// C贸digo que lanza una excepci贸n | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Mensaje de error: " + ex.Message); | |
Console.WriteLine("Traza de pila: " + ex.StackTrace); | |
if (ex.InnerException != null) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<string> ObtenerDatosAsync() | |
{ | |
try | |
{ | |
return await ClienteHttp.GetStringAsync("https://api.example.com/data"); | |
} | |
catch (HttpRequestException ex) | |
{ | |
Console.WriteLine("Error al obtener los datos: " + ex.Message); | |
return null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
// C贸digo que puede lanzar excepciones | |
} | |
catch (Exception ex) | |
{ | |
Log.Error(ex, "Error inesperado en la operaci贸n."); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var circuitBreakerPolicy = Policy | |
.Handle<SqlException>() | |
.CircuitBreaker(2, TimeSpan.FromMinutes(1)); | |
circuitBreakerPolicy.Execute(() => AccesoBaseDeDatos()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var policy = Policy | |
.Handle<HttpRequestException>() | |
.WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); | |
policy.Execute(() => RealizarLlamadaHttp()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int retries = 3; | |
while (retries > 0) | |
{ | |
try | |
{ | |
// C贸digo que puede fallar | |
break; // Si tiene 茅xito, salir del bucle | |
} | |
catch (NetworkException) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
RealizarOperacionNegocio(); | |
} | |
catch (BusinessException ex) | |
{ | |
Console.WriteLine("Error de negocio: " + ex.Message); | |
if (ex.InnerException != null) | |
{ | |
Console.WriteLine("Excepci贸n original: " + ex.InnerException.Message); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
// Simulaci贸n de fallo en la base de datos | |
throw new SqlException("Error en la base de datos."); | |
} | |
catch (SqlException ex) | |
{ | |
// Encadenamiento de la excepci贸n original en una nueva excepci贸n de negocio | |
throw new BusinessException("Fallo al realizar la operaci贸n de negocio.", ex); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (amount < 0) | |
{ | |
throw new InvalidTransactionException("La cantidad no puede ser negativa."); | |
} |