Added new section (#28311)
Added new section about getting the message inside an exceptionpull/36473/head
parent
c8df1d15d0
commit
ca4179aa85
|
@ -8,7 +8,7 @@ An exception is an unexpected error that occurs while a program is running, such
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
If we try to read the text of a file that does not exist:
|
If we try to read the text of a file that does not exist:
|
||||||
```
|
```csharp
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
string content = File.ReadAllText(@"C:\DoesNotExist.txt");
|
string content = File.ReadAllText(@"C:\DoesNotExist.txt");
|
||||||
|
@ -22,11 +22,31 @@ Some other common exceptions:
|
||||||
* `NullReferenceException`: Attempted to use an unassigned reference variable.
|
* `NullReferenceException`: Attempted to use an unassigned reference variable.
|
||||||
* `DivideByZeroException`: Attempted to divide by 0.
|
* `DivideByZeroException`: Attempted to divide by 0.
|
||||||
|
|
||||||
|
## Get The Message Inside An Exception
|
||||||
|
Whenever you use the generic `Exception` catch all handler, you can grab the message of what caused the error each time. It is very simple to do so:
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string content = File.ReadAllText(@"C:\DoesNotExist.txt");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
string message = "";
|
||||||
|
if (ex.InnerException != null) {
|
||||||
|
message = ex.InnerException.Message;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
message = ex.Message;
|
||||||
|
}
|
||||||
|
Console.WriteLine(message);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Best Practices
|
## Best Practices
|
||||||
|
|
||||||
### Use try/catch/finally Blocks
|
### Use try/catch/finally Blocks
|
||||||
```
|
```csharp
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var client = new WebClient();
|
var client = new WebClient();
|
||||||
|
@ -46,7 +66,7 @@ finally
|
||||||
|
|
||||||
Instead of
|
Instead of
|
||||||
|
|
||||||
```
|
```csharp
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
conn.Close();
|
conn.Close();
|
||||||
|
@ -59,9 +79,11 @@ catch (Exception ex)
|
||||||
|
|
||||||
Try this
|
Try this
|
||||||
|
|
||||||
```
|
```csharp
|
||||||
if (conn.State != ConnectionState.Closed)
|
if (conn.State != ConnectionState.Closed)
|
||||||
{
|
{
|
||||||
conn.Close();
|
conn.Close();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue