Skip to content

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);
  }
}

Exception Filters

Use when to conditionally run code in a catch block.

try {}
catch (Exception e) when (isDebuggingOn) {}
Clone this wiki locally