try() catch() finally()
Telerik Academy Alpha
Table of contents
What are exceptions?
What is an exception?
-
An exception breaks the normal flow of execution
-
One mechanism to transfer control, or raise an exception, is known as a throw
-
Execution is transferred to a "catch"
-
-
Exceptions in .NET
-
Centralized mechanism for handling errors or unusual events
-
Substitute procedure-oriented approach
-
-
Simplify code construction
-
Handling Exceprions
Handling Exceptions
-
In C# exceptions are handled with
try-catch-finally blocks
try
{
// Do something
}
catch (Exception)
{
// Do something with the catched exception
}
finally
{
// Executed always but not mandatory
}
Handling Exceptions
-
There could be multiple catch clauses
-
The exceptions should be ordered from derived to base
-
Do not catch generic exceptions (ex: Exception)
-
-
try
{
throw new Exception();
}
catch (ArgumentNullException)
{
Console.WriteLine("ArgumentNUllException");
}
catch (Exception)
{
Console.WriteLine("Exception");
}
Exception class
- Exceptions in .NET are objects
- Exception is the base for all exceptions in CLR
- Contain information for the cause of the error
- And some other useful information
- Message
- StackTrace
- InnerException
- Source
- etc.
- And some other useful information
Exceptions hierarchy
-
The exceptions in .NET are organized in a hierarchy
-
Don not catch Exception class
-
Just for debugging
-
-
Exceptions hierarchy
-
When you catch an exception from a particular class
-
All its ancestors are also caugth
-
try
{
throw new ArgumentNullException("Null object");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Exception
ArgumentNullException
Base class
Derived class
More about classes (stay tuned)
Finally
-
Ensures that a given block of code will be executed
-
No matter what happens
-
-
You could release(dispose) resources(objects) there
try
{
int a = 5;
int b = a / 0; // Exception
}
catch (DivideByZeroException e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Executed always");
}
Throwing Exceptions
Throwing Exceptions
- Exceptions are thrown using throw keyword in C#
-
When the exception is thrown
- the execution of the program stops
- The exception travels over the stack until a suitable catch block is reached to handle it
- Unhandled exceptions display error message
Throwing Exceptions
- Throw an exception with custom error message
try
{
throw new ArgumentException("Custom message");
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
}
Throwing Exceptions
- Throw an exception with custom error message
- and inner exception
try
{
var exception = new ArgumentException("My inner exception");
throw new NullReferenceException("Custom message", exception);
}
catch (NullReferenceException e)
{
Console.WriteLine(e.InnerException);
}
Re-Throwing Exceptions
- Caught exceptions can be re-thrown again
try
{
int.Parse(str);
}
catch (FormatException fe)
{
Console.WriteLine("Parse failed!");
throw fe; // Re-throw the caught exception
}
Best practices
Throwing Exceptions
-
catch blocks should begin with the exceptions lowest in the hierarchy
- And continue with the more general exceptions
-
Otherwise a compilation error will occur
- Each catch block should handle only these exceptions which it expects
Throwing Exceptions
-
When raising an exception always pass to the constructor good explanation message
-
When throwing an exception always pass a good description of the problem
-
Exceptions can decrease the application performance
- Throw exceptions only in situations which are really exceptional and should be handled
- Do not throw exceptions in the normal program control flow (e.g. for invalid user input)
Questions?
[C#] Exception handling
By telerikacademy
[C#] Exception handling
- 999