static void Main(string[] args) { try { Transport transport3 = new Transport { Price = -15000 }; } catch (Exceptions.PriceException ex) // универсальный обработчик catch { Console.WriteLine("Ошибка: " + ex.Message); Console.WriteLine("Метод: " + ex.TargetSite); Console.WriteLine("Источник: " + ex.Source); } Console.Read(); try { Transport transport = new Transport { MaxSpeed = 2500 }; } catch (Exceptions.SpeedException ex) // универсальный обработчик catch { Console.WriteLine("Ошибка: " + ex.Message); Console.WriteLine($"Некорректное значение: {ex.Value}"); Console.WriteLine("Метод: " + ex.TargetSite); Console.WriteLine("Источник: " + ex.Source); } Console.Read(); Transport tr = new Transport(); try { int res = tr.Volume / 0; } catch (Exception ex) { Console.WriteLine($"Ошибка: {ex.Message}"); Console.WriteLine("Метод: " + ex.TargetSite); Console.WriteLine("Источник: " + ex.Source); } Console.Read(); Transport tra = new Transport(); tra = null; // новый объект типа transport равный null TransportAgency ag = new TransportAgency(); try { ag.Add(tra); } catch (Exceptions.NotImplement ex) { Console.WriteLine("Ошибка: " + ex.Message); Console.WriteLine("Метод: " + ex.TargetSite); Console.WriteLine("Источник: " + ex.Source); } Console.Read(); try { Console.WriteLine(ag._container[3]); } catch (ArgumentOutOfRangeException ex) { Console.WriteLine("Ошибка: " + ex.Message); Console.WriteLine("Метод: " + ex.TargetSite); Console.WriteLine("Источник: " + ex.Source); } Console.Read(); try { int n = 5; int r = n / 0; Console.WriteLine(ag._container[-7]); } catch { Console.WriteLine("Возникли некоторые ошибки"); } finally { Console.WriteLine("Блок finally"); // отлавливает все исключения } }
static void Main(string[] args) { Transport transport = new Transport("transport", 10000, 60, 120, 8); transport.DoClone(); ((ICloneable)transport).DoClone(); ((ICloneable1)transport).DoClone(); Transport.Car car = new Transport.Car("car", 20000, 55, 100, 8); Transport.Train train = new Transport.Train(222); Transport.Car.Motor motor = new Transport.Car.Motor(30); Transport.Train.Motor motor1 = new Transport.Train.Motor(40); Transport.Train.Express express = new Transport.Train.Express(555); Transport.Train.Carriage carriage = new Transport.Train.Carriage(60); if (train is Transport) { Console.WriteLine("Train is transport"); } else { Console.WriteLine("Train is not transport"); } Transport.Car car1 = transport as Transport.Car; if (car1 == null) { Console.WriteLine("Преобразование прошло неудачно"); } else { Console.WriteLine("Преобразование прошло удачно"); } dynamic[] arrayOfTransport = new dynamic[] { train, transport, carriage, car }; Printer printer = new Printer(); printer.IAmPrinting(train); printer.IAmPrinting(car); printer.IAmPrinting(carriage); Transport transport1 = new Transport("transport", 15000, 80, 120, 9); Transport transport2 = new Transport("transport", 17000, 80, 120, 7); Transport.Car car2 = new Transport.Car("car", 30000, 120, 200, 2); TransportAgency agency = new TransportAgency(); agency.AddItem(car); agency.AddItem(transport1); agency.AddItem(transport2); agency.AddItem(car2); agency.Print(); TransportController.Sum(agency._container); TransportController.SortByFuel(agency._container); TransportController.BySpeed(agency._container); Transport.Structura structura = new Transport.Structura("text", 5); Console.WriteLine(structura); Console.WriteLine(Transport.Age.Old); }