Skip to content
Justice Tech School

throw and Custom Exceptions

throw raises an exception. Derive your own from Exception.

Example
static void Check(int a) { if (a < 0) throw new AgeException("negative"); }
try { Check(-1); } catch (AgeException e) { Console.WriteLine(e.Message); }

class AgeException(string msg) : Exception(msg);

Exercise

Raise.

 new Exception("x");

Quiz Test

What do custom exceptions derive from?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.