<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=51194&amp;view=findpost&amp;p=2231541</guid>
        <pubDate>Mon, 30 Mar 2009 14:50:16 +0000</pubDate>
        <title>Обработка исключительных ситуаций</title>
        <link>https://forum.sources.ru/index.php?showtopic=51194&amp;view=findpost&amp;p=2231541</link>
        <description><![CDATA[AndNot: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=51194&view=findpost&p=1737406'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>SkyStar &#064; <time class="tag-quote__quoted-time" datetime="2007-10-21T21:33:30+04:00">21.10.07, 17:33</time></span><div class='quote '>В Pascal нельзя никак отлавливать исключения. </div></div>Никогда не говори - никогда :D <br>
Можно создать некий аналог try..except, несколько расширив стандартную возможность перехвата исключений - <em class='tag-i'>ExitProc</em>:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">unit Except;</div><div class="code_line">&nbsp;</div><div class="code_line">interface</div><div class="code_line">type</div><div class="code_line">&nbsp;&nbsp;Try = record</div><div class="code_line">&nbsp;&nbsp; &nbsp;_sp, _bp: Word;</div><div class="code_line">&nbsp;&nbsp; &nbsp;_farptr: Pointer;</div><div class="code_line">&nbsp;&nbsp;end;</div><div class="code_line">&nbsp;</div><div class="code_line">procedure SetExcept(var dest: Try); inline (</div><div class="code_line">f///////d//</div><div class="code_line">$e8/{text}/{text}///{text}c/{text}///</div><div class="code_line">///c/d/);</div><div class="code_line">&nbsp;</div><div class="code_line">procedure ProceedExcept(var dest: Try); inline (</div><div class="code_line">f///b///b/d//</div><div class="code_line">/$ff/d/);</div><div class="code_line">&nbsp;</div><div class="code_line">implementation</div><div class="code_line">&nbsp;</div><div class="code_line">end.</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script> <br>
а это пример:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">uses dos, crt, except;</div><div class="code_line">&nbsp;</div><div class="code_line">var</div><div class="code_line">&nbsp;&nbsp;OldExit: Pointer;</div><div class="code_line">&nbsp;&nbsp;exc &nbsp; &nbsp;: Try;</div><div class="code_line">&nbsp;&nbsp;r &nbsp; &nbsp; &nbsp;: Real;</div><div class="code_line">label</div><div class="code_line">&nbsp;&nbsp;l;</div><div class="code_line">&nbsp;</div><div class="code_line">procedure NewExit; far;</div><div class="code_line">begin</div><div class="code_line">&nbsp;&nbsp;ExitProc := @NewExit;</div><div class="code_line">&nbsp;&nbsp;ProceedExcept(exc);</div><div class="code_line">end;</div><div class="code_line">&nbsp;</div><div class="code_line">begin</div><div class="code_line">&nbsp;&nbsp;OldExit := ExitProc;</div><div class="code_line">&nbsp;&nbsp;ExitProc := @NewExit;</div><div class="code_line">&nbsp;&nbsp;repeat</div><div class="code_line">&nbsp;&nbsp;l:</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;Writeln;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;Write(&#39;Enter number: &#39;);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;SetExcept(exc);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;Readln(r);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;ErrorAddr := NIL;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;SetExcept(exc);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;if ErrorAddr &#60;&#62; NIL then</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Goto l;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;Writeln(&#39;100/&#39;,r,&#39;= &#39;,100.0/r);</div><div class="code_line">&nbsp;&nbsp;until (r=1);</div><div class="code_line">&nbsp;&nbsp;ExitProc := OldExit;</div><div class="code_line">end.</div></ol></div></div></div></div><br>
Данный примерчик довольно трудно уронить, он вполне спокойно обруливает неверный ввод числа, а так же деление на ноль. <br>
Но модуль except не идеален, его нужно немного дополнить, что бы иметь возможность восстанавливать программу даже в особо тяжелых случаях, когда важны значения всех регистров. Но для примера сойдет :)<br>
<br>
<span class="tag-color tag-color-named" data-value="gray" style="color: gray"><span class='tag-size' data-value='7' style='font-size:7pt;'>Это сообщение было перенесено сюда или объединено из темы &quot;как отлаливать исключение в турбо паскале 7.0&quot;</span></span>]]></description>
        <author>AndNot</author>
        <category>Pascal: Общие вопросы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=51194&amp;view=findpost&amp;p=337025</guid>
        <pubDate>Mon, 12 Apr 2004 20:44:28 +0000</pubDate>
        <title>Обработка исключительных ситуаций</title>
        <link>https://forum.sources.ru/index.php?showtopic=51194&amp;view=findpost&amp;p=337025</link>
        <description><![CDATA[romtek: <span class='tag-size' data-value='12' style='font-size:12pt;'>Обработка исключительных ситуаций</span><br>
Модуль с примером из книги Фаронова В.В.<br>
<br>
Решает проблемы ошибки деления на ноль, критических ошибок ввода/вывода.<br>
<br>
<strong class='tag-b'><span class='tag-size' data-value='10' style='font-size:10pt;'>Пример использования</span></strong><br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">{+-------------------------------------------------------+</div><div class="code_line">&nbsp;| Программа иллюстрирует механизм выхода из процедуры &nbsp; |</div><div class="code_line">&nbsp;| &nbsp; завершения в нужную точку программы.В программе &nbsp; &nbsp; |</div><div class="code_line">&nbsp;| осуществляется ввод произвольного целого числа, затем |</div><div class="code_line">&nbsp;| &nbsp; вычисляется корень квадратный из него и находится &nbsp; |</div><div class="code_line">&nbsp;| обратная ему величина.Для завершения работы программы |</div><div class="code_line">&nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; следует ввести единицу. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |</div><div class="code_line">&nbsp;|+------------------------------------------------------+}</div><div class="code_line">Uses F_Jump;</div><div class="code_line">var</div><div class="code_line">&nbsp;&nbsp;OldExit: Pointer; &nbsp;{Старое значение переменной ExitProc}</div><div class="code_line">&nbsp;&nbsp;Jump &nbsp; : JumpRec; &nbsp;{Сохранение точки выхода}</div><div class="code_line">&nbsp;&nbsp;rr &nbsp; &nbsp; &nbsp;: Real; &nbsp; &nbsp; {Вспомогательная переменная}</div><div class="code_line">label</div><div class="code_line">&nbsp;&nbsp;Loop;</div><div class="code_line">Procedure MyExit; Far;</div><div class="code_line">&nbsp;&nbsp;{Эта процедура обрабатывает исключительную ситуацию}</div><div class="code_line">begin</div><div class="code_line">&nbsp;&nbsp;LongJump(Jump)</div><div class="code_line">end; &nbsp;{MyExit}</div><div class="code_line">begin</div><div class="code_line">&nbsp;&nbsp;OldExit := ExitProc;</div><div class="code_line">&nbsp;&nbsp;repeat</div><div class="code_line">Loop: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {Точка повторения ввода}</div><div class="code_line">&nbsp;&nbsp; &nbsp;if ErrorAddr &#60;&#62; NIL then</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;begin</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Write(&#39;Повторите ввод числа: &#39;);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ErrorAddr := NIL;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ExitCode := 0</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;end</div><div class="code_line">&nbsp;&nbsp; &nbsp;else</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;Write(&#39;Введите число: &#39;);</div><div class="code_line">&nbsp;&nbsp; &nbsp;ExitProc := @MyExit; &nbsp; &nbsp; &nbsp; &nbsp;{Адрес процедуры выхода}</div><div class="code_line">&nbsp;&nbsp; &nbsp;SetJump(Jump); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{Контролируем ввод данных}</div><div class="code_line">&nbsp;&nbsp; &nbsp;if ErrorAddr &#60;&#62; NIL then</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;begin</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;WriteLn(&#39;Ошибка при вводе вещественного числа&#39;);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Goto Loop</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;end</div><div class="code_line">&nbsp;&nbsp; &nbsp;else</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;ReadLn(rr);</div><div class="code_line">&nbsp;&nbsp; &nbsp;SetJump(Jump); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {Контролируем извлечение корня}</div><div class="code_line">&nbsp;&nbsp; &nbsp;if ErrorAddr &#60;&#62; NIL then</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;begin</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;WriteLn(&#39;Ошибка при извлечении корня&#39;);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Goto Loop</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;end</div><div class="code_line">&nbsp;&nbsp; &nbsp;else</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;rr := sqrt(rr);</div><div class="code_line">&nbsp;&nbsp; &nbsp;SetJump(Jump); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{Контролируем деление}</div><div class="code_line">&nbsp;&nbsp; &nbsp;if ErrorAddr &#60;&#62; NIL then</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;begin</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;WriteLn(&#39;Ошибка при делении чисел&#39;);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Goto Loop</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;end</div><div class="code_line">&nbsp;&nbsp; &nbsp;else</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp;rr := 1/rr;</div><div class="code_line">&nbsp;&nbsp; &nbsp;WriteLn(r:20:10)</div><div class="code_line">&nbsp;&nbsp;until rr = 1;</div><div class="code_line">&nbsp;&nbsp;ExitProc := OldExit &nbsp; &nbsp; &nbsp; {Нормальный выход}</div><div class="code_line">end.</div></ol></div></div></div></div>]]></description>
        <author>romtek</author>
        <category>Pascal: Общие вопросы</category>
      </item>
	
      </channel>
      </rss>
	