Skip to content

Instantly share code, notes, and snippets.

View javierguerrero's full-sized avatar
馃彔
Working from home

Javier Guerrero javierguerrero

馃彔
Working from home
View GitHub Profile
@javierguerrero
javierguerrero / exception_handling_csharp_chapter6_2.cs
Created November 8, 2024 22:02
exception_handling_csharp_chapter6_2.cs
try
{
MetodoCritico();
}
catch (Exception ex)
{
Console.WriteLine($"Error en MetodoCritico: {ex.Message}");
throw; // Relanza la excepci贸n despu茅s de registrar detalles
}
@javierguerrero
javierguerrero / exception_handling_csharp_chapter6_1.cs
Created November 8, 2024 21:59
exception_handling_csharp_chapter6_1.cs
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)
{
@javierguerrero
javierguerrero / exception_handling_csharp_chapter5_9.cs
Created October 28, 2024 20:13
exception_handling_csharp_chapter5_9.cs
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;
@javierguerrero
javierguerrero / exception_handling_csharp_chapter5_8.cs
Created October 28, 2024 20:12
exception_handling_csharp_chapter5_8.cs
try
{
// C贸digo que puede lanzar excepciones
}
catch (Exception ex)
{
Log.Error(ex, "Error inesperado en la operaci贸n.");
}
@javierguerrero
javierguerrero / exception_handling_csharp_chapter5_7.cs
Created October 28, 2024 20:09
exception_handling_csharp_chapter5_7.cs
var circuitBreakerPolicy = Policy
.Handle<SqlException>()
.CircuitBreaker(2, TimeSpan.FromMinutes(1));
circuitBreakerPolicy.Execute(() => AccesoBaseDeDatos());
@javierguerrero
javierguerrero / exception_handling_csharp_chapter5_6.cs
Created October 28, 2024 20:08
exception_handling_csharp_chapter5_6.cs
var policy = Policy
.Handle<HttpRequestException>()
.WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
policy.Execute(() => RealizarLlamadaHttp());
@javierguerrero
javierguerrero / exception_handling_csharp_chapter5_5.cs
Created October 28, 2024 20:05
exception_handling_csharp_chapter5_5.cs
int retries = 3;
while (retries > 0)
{
try
{
// C贸digo que puede fallar
break; // Si tiene 茅xito, salir del bucle
}
catch (NetworkException)
{
@javierguerrero
javierguerrero / exception_handling_csharp_chapter5_4.cs
Created October 28, 2024 20:00
exception_handling_csharp_chapter5_4.cs
try
{
RealizarOperacionNegocio();
}
catch (BusinessException ex)
{
Console.WriteLine("Error de negocio: " + ex.Message);
if (ex.InnerException != null)
{
Console.WriteLine("Excepci贸n original: " + ex.InnerException.Message);
@javierguerrero
javierguerrero / exception_handling_csharp_chapter5_3.cs
Created October 28, 2024 19:59
exception_handling_csharp_chapter5_3.cs
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);
}
@javierguerrero
javierguerrero / exception_handling_csharp_chapter5_2.cs
Created October 28, 2024 19:55
exception_handling_csharp_chapter5_2.cs
if (amount < 0)
{
throw new InvalidTransactionException("La cantidad no puede ser negativa.");
}