tag:blogger.com,1999:blog-6770389.post393231255635956169..comments2023-01-07T20:35:03.233+03:00Comments on DkmS's blog: Бойся Try/Catch…DkmShttp://www.blogger.com/profile/03575308325717695654noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-6770389.post-47285137207686824872012-12-29T13:30:06.739+04:002012-12-29T13:30:06.739+04:00Спасибо, Александр - полезно про ошибки, буду испо...Спасибо, Александр - полезно про ошибки, буду использовать.<br />Но зачем же так, через универсальный интерфейс, сделано...DkmShttps://www.blogger.com/profile/03575308325717695654noreply@blogger.comtag:blogger.com,1999:blog-6770389.post-7365301292268558732012-12-29T13:09:24.412+04:002012-12-29T13:09:24.412+04:00Есть два вида ошибок: terminating errors и non-ter...Есть два вида ошибок: terminating errors и non-terminating errors.<br />Try/catch ловит только terminating errors. <br />Есть два варианта, чтобы exception не вываливался:<br />1) отключить выдачу exception при выполнении команды:<br />Remove-Item "c:\example.docx" -ea 0<br />2)конвертнуть non-terminating error в terminating error - нужно установить параметр ErrorAction с значением stop.<br />Например:<br />try { Remove-Item "c:\example.docx" -ea stop } catch { "Test" }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6770389.post-45833404218798173702012-12-29T13:08:03.854+04:002012-12-29T13:08:03.854+04:00Есть два вида ошибок: terminating errors и non-ter...Есть два вида ошибок: terminating errors и non-terminating errors.<br />Try/catch ловит только terminating errors. <br />Есть два варианта, чтобы exception не вываливался:<br />1) отключить выдачу exception при выполнении команды:<br />Remove-Item "c:\example.docx" -ea 0<br />2)конвертнуть non-terminating error в terminating error - нужно установить параметр ErrorAction с значением stop.<br />Например:<br />try { Remove-Item "c:\example.docx" -ea stop } catch { "Test" }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6770389.post-81193659900349428352012-12-28T19:58:24.159+04:002012-12-28T19:58:24.159+04:00C нулём, конечно, пример искусственный (хотя для е...C нулём, конечно, пример искусственный (хотя для единообразия могли бы и сделать нормальное исключение), а что там говорят про другие случаи?<br />Я тут натолкнулся на то, что не ловится попытка удалить несуществующий файл (при помощи Remove-Item)...DkmShttps://www.blogger.com/profile/03575308325717695654noreply@blogger.comtag:blogger.com,1999:blog-6770389.post-82763109148308038062012-12-28T18:04:38.845+04:002012-12-28T18:04:38.845+04:00На форуме по powershell так объяснили из-за чего c...На форуме по powershell так объяснили из-за чего catch не срабатывает:<br />"The example here uses 1/$null. The reason for doing this instead of simply <br />1/0 is because the PowerShell interpreter does something called constant <br />expression folding.<br /><br />It looks at expressions that contain only constant values. When it sees one, <br />it evaluates that expression once at compile time so it doesn’t have to waste <br />time doing it again at runtime.<br /><br />This means that impossible expressions, such as division by zero, are caught and treated as<br />parsing errors. Parsing errors can’t be caught and don’t get logged when they’re entered<br />interactively, so they don’t make for a good example. (If one script calls another script and that<br />script has one of these errors, the calling script can catch it, but the script being parsed cannot.)"Anonymousnoreply@blogger.com