Commit 89e3f14c authored by Trần Duy Truyền 's avatar Trần Duy Truyền

chuong 4

parent dcf30765
...@@ -11,18 +11,11 @@ namespace TrainingCSharp ...@@ -11,18 +11,11 @@ namespace TrainingCSharp
switch (action) switch (action)
{ {
case 1: case 1:
//Nạp chồng toán tử RethrowException.Run();
var vector1 = new Vector2D { x = 1, y = 2 };
var vector2 = new Vector2D { x = 3, y = 4 };
var vector3 = vector1 + vector2;
Console.WriteLine(vector3.ToString());
break; break;
case 2: case 2:
//Indexer //Indexer
var vector1Indexer = new Vector2DIndexer();
vector1Indexer[0] = 1;
vector1Indexer[1] = 2;
Console.WriteLine(vector1Indexer[0].ToString()+ vector1Indexer[1].ToString());
break; break;
default: default:
Console.WriteLine("Hello World!"); Console.WriteLine("Hello World!");
......
class Vector2DIndexer
{
public double x, y;
public double this[int i]
{
get
{
if (i == 0) return x;
else if (i == 1) return y;
else { throw new IndexOutOfRangeException(); }
}
set
{
if (i == 0) x = value;
else if (i == 1) y = value;
else { throw new IndexOutOfRangeException(); }
}
}
}
//Truy cập các phần tử của object như mảng
\ No newline at end of file
class Vector2D
{
public double x, y;
public static Vector2D operator +(Vector2D a, Vector2D b)
{
return new Vector2D { x = a.x + b.x, y = a.y + b.y };
}
public override string ToString()
{
return $"({x},{y})";
}
}
//Thường dùng trong toán học
public class TryCatch
{
public static void Run()
{
int a = 10;
int b = 0;
try
{
int c = a / b;
}
catch (DivideByZeroException e)
{
Console.WriteLine("Error: " + e.Message);
}
finally
{
Console.WriteLine("Finally block");
}
}
}
// CustomException
public class MyCustomException : Exception
{
public string CustomProperty { get; set; }
public MyCustomException(string message, string customProperty) : base(message)
{
CustomProperty = customProperty;
}
}
// Rethrow exception
// Luôn sử dụng throw; thay vì throw ex; để bảo toàn stack trace
public class RethrowException
{
public static void Run()
{
int a = 10;
int b = 0;
try
{
try
{
int c = a / b;
}
catch (DivideByZeroException e)
{
// Console.WriteLine("Error: " + e.Message);
throw;
}
}
catch (System.Exception e)
{
System.Console.WriteLine("Error: " + e.Message); ;
}
finally
{
Console.WriteLine("Finally block");
}
}
}
public class ExceptionFilter
{
public static void Run()
{
try
{
}
catch (System.Exception e) when (e is IOException || e is ArgumentNullException)
{
throw;
}
}
}
// Global Exception Handling
//ASP.NET Core: Sử dụng Middleware
//Ứng dụng Desktop: Sử dụng AppDomain.CurrentDomain.UnhandledException hoặc TaskScheduler.UnobservedTaskException.
// Logging và Monitoring
// Nội dung log nên có:
// Thời gian xảy ra lỗi: Để theo dõi trình tự và tần suất lỗi.
// Loại ngoại lệ: (ex.GetType().FullName)
// Thông báo lỗi: (ex.Message)
// Stack Trace: (ex.StackTrace) - Đặc biệt quan trọng để gỡ lỗi.
// Dữ liệu liên quan: (Ví dụ: OrderId trong OrderProcessingException, thông tin người dùng, dữ liệu đầu vào gây lỗi, v.v.)
// Context: (Ví dụ: Tên phương thức, lớp, module nơi xảy ra lỗi)
// Retry Pattern:
//Khi thực hiện các thao tác không ổn định (như gọi API bên ngoài), bạn có thể thử lại vài lần trước khi báo lỗi.
// Circuit Breaker:
//Ngăn chặn việc gọi các dịch vụ khi chúng liên tục trả về lỗi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment