例外:这是个好习惯吗? [英] Exceptions: Is this a good practice?

查看:145
本文介绍了例外:这是个好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 尝试
{
try
{
$ issue = new DM_Issue($ core-> db-> escape_string($ _ GET ['issue']));
}
catch(DM_Exception $ e)
{
throw new Error_Page($ tpl,ERR_NOT_FOUND,$ e-> getMessage());
}
}
catch(Error_Page $ e)
{
die($ e);
}

嵌套尝试,抓住一个好的做法遵循?似乎有点笨重只是一个错误页面 - 但是我的问题Datamanager抛出异常,如果发生错误,我认为这是一个很好的错误检测方式。



Error_Page异常只是一个错误页面编译器。



我可能只是迂腐,但是你认为这是一个很好的报告错误的方法,如果可以,你建议一个谢谢

解决方案

你是使用异常页面逻辑,我个人认为这不是一件好事。在发生不良或意外事件时,应使用异常信号,不要控制错误页面的输出。如果要根据异常生成错误页面,请考虑使用 set_exception_handler 。任何未捕获的异常通过您指定的任何回调方法运行。请记住,这不会阻止异常的致命。在通过回调传递异常之后,在任何未捕获的异常之后,执行将像正常一样停止。


This is written in PHP, but it's really language agnostic.

try
{
    try
    {
        $issue = new DM_Issue($core->db->escape_string($_GET['issue']));
    }
    catch(DM_Exception $e)
    {
        throw new Error_Page($tpl, ERR_NOT_FOUND, $e->getMessage());
    }
}
catch(Error_Page $e)
{
    die($e);
}

Is nested try, catch blocks a good practice to follow? It seems a little bulky just for an error page - however my Issue Datamanager throws an Exception if an error occurs and I consider that to be a good way of error detecting.

The Error_Page exception is simply an error page compiler.

I might just be pedantic, but do you think this is a good way to report errors and if so can you suggest a better way to write this?

Thanks

解决方案

You're using Exceptions for page logic, and I personally think that's not a good thing. Exceptions should be used to signal when bad or unexpected things happen, not to control the output of an error page. If you want to generate an error page based on Exceptions, consider using set_exception_handler. Any uncaught exceptions are run through whatever callback method you specify. Keep in mind that this doesn't stop the "fatalness" of an Exception. After an exception is passed through your callback, execution will stop like normal after any uncaught exception.

这篇关于例外:这是个好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆