Throw an exception in C#
Forums:
class Car {}
class Seller {
List<Car> cars = new List<Car>();
public void Sell() {
if (cars.Count == 0) {
throw new Exception("No cars for sale");
}
//some implementation...
}
}
var seller = new Seller();
try {
seller.Sell(); //<-Error
}
catch (Exception e) {
Console.WriteLine(e.Message);
//e.Message is "No cars for sale"
}
- Log in to post comments