Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
training-csharp
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Trần Duy Truyền
training-csharp
Commits
89e3f14c
Commit
89e3f14c
authored
Feb 18, 2025
by
Trần Duy Truyền
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chuong 4
parent
dcf30765
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
96 additions
and
45 deletions
+96
-45
Program.cs
Program.cs
+2
-9
indexder.cs
indexder.cs
+0
-23
operator-overloading.cs
operator-overloading.cs
+0
-13
try-catch.cs
try-catch.cs
+94
-0
No files found.
Program.cs
View file @
89e3f14c
...
...
@@ -11,18 +11,11 @@ namespace TrainingCSharp
switch
(
action
)
{
case
1
:
//Nạp chồng toán tử
var
vector1
=
new
Vector2D
{
x
=
1
,
y
=
2
};
var
vector2
=
new
Vector2D
{
x
=
3
,
y
=
4
};
var
vector3
=
vector1
+
vector2
;
Console
.
WriteLine
(
vector3
.
ToString
());
RethrowException
.
Run
();
break
;
case
2
:
//Indexer
var
vector1Indexer
=
new
Vector2DIndexer
();
vector1Indexer
[
0
]
=
1
;
vector1Indexer
[
1
]
=
2
;
Console
.
WriteLine
(
vector1Indexer
[
0
].
ToString
()+
vector1Indexer
[
1
].
ToString
());
break
;
default
:
Console
.
WriteLine
(
"Hello World!"
);
...
...
indexder.cs
deleted
100644 → 0
View file @
dcf30765
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
operator-overloading.cs
deleted
100644 → 0
View file @
dcf30765
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
try-catch.cs
0 → 100644
View file @
89e3f14c
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment