-
Notifications
You must be signed in to change notification settings - Fork 1
Exceptions
Mario Gutierrez edited this page Jan 7, 2017
·
1 revision
Fatal system exceptions extend SystemException
and are thrown by the CLR. Extend
ApplicationException
, or just Exception
, for app-related exceptions.
You don't have to specify an exception in try-catch.
try { }
catch { }
If you do, the syntax is the same as in Java.
try { }
catch (Exception e) { }
You can rethrow an exception by just calling throw in the catch block.
try {}
catch
{
// Stuff.
throw;
}
Throwing exceptions is the same as in Java too.
throw new SomeException();
If an exception happens while handling another, the proper practice is to shove the inner exception in a new one of the parent type.
try { }
catch (CustomException ce)
{
try { }
catch (Exception ie)
{
throw new CustomException(ce.Message, ie);
}
}
Use when
to conditionally run code in a catch block.
try {}
catch (Exception e) when (isDebuggingOn) {}
- Abstract Classes
- Access Modifiers
- Anonymous Methods
- Anonymous Types
- Arrays
- Attributes
- Console I/O
- Constructors
- Const Fields
- Delegates
- Enums
- Exceptions
- Extension Methods
- File IO
- Generics
- Interfaces
- Iterators
- LINQ
- Main
- Null Operators
- Parameters
- Polymorphism
- Virtual Functions
- Reflection
- Serialization
- Strings
- Value Types
- "Base" Keyword
- "Is" and "As"
- "Sealed" Keyword
- nameof expression