`

PHP异常处理

    博客分类:
  • PHP
 
阅读更多

 

foreach ($uploadImages as $key => $uploadImage) {
    try {
        if (strpos($uploadImage['type'], 'image/') !== false) {
            $fileId     = FileAccessor::uploadFile($uploadImage, 'image', SITE_ID);
        } else {
            $fileId     = FileAccessor::uploadFile($uploadImage, 'flash', SITE_ID);
        }

        $arr['pictures'][]  =  $fileId;
        $arr['hrefs'][] = $newHrefs[$key];
        $arr['hrefs2'][] = $newHrefs2[$key];
        $arr['hrefs3'][] = $newHrefs3[$key];
    } catch (FileException $e) {
        throw new WidgetException($e->getMessage());
    }
}

#########################################################################

public static function expandAttributes( $attribs ) {
    $out = '';
    if( is_null( $attribs ) ) {
	    return null;
    } elseif( is_array( $attribs ) ) {
	    foreach( $attribs as $name => $val ) {
		    $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
	    }
	    return $out;
    } else {
	    throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
    }
}

#########################################################################

public static function newFromText( $text, $defaultNamespace = NS_MAIN ) {
	if ( is_object( $text ) ) {
		throw new MWException( 'Title::newFromText given an object' );
	}
	
	......
}

#########################################################################

protected static function save($setting)
{
    try {
        $setting->save();
    } catch (FieldException $e) {
        if ($e->getCode() == FieldException::UNIQUE_VALUE) {
            throw ControllerException(
                '对不起,您的店铺已经存在,请您刷新后编辑。'
            );
        } elseif ($e->getCode() == FieldException::BLANK_VALUE) {
            switch ($e->field) {
            case 'shopName':
                throw new ControllerException(
                    '对不起,店铺名称不能为空。'
                );
            case 'shopTitle':
                throw new ControllerException(
                    '对不起,店铺标题不能为空。'
                );
            default:
                throw $e;
            }
        } else {
            throw $e;
        }
    }
}

#########################################################################

public function connect()
{
    if (null == $this->_connId) {
        try {
            $this->_connId = mysql_connect($this->_dbHost, $this->_dbUser, $this->_dbPass);
        
            mysql_query(
                'SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary', 
                $this->_connId
            );
        } catch (ErrorException $e) {
            throw new DatabaseException(
                "Connect to the server $this->_dbHost is failed." . mysql_error(), 
                DatabaseException::ERR_CONNECT
            );
        }
    }
    
    return $this;
}

#########################################################################

$dbw = wfGetDB( DB_MASTER );
$dbw->begin();

try {
	$status = $article->doEdit( $text, $summary, $flags, false, $editor );
} catch ( DBQueryError $e ) {
	$status = Status::newFatal("DB Error");
}

if ( $status->isGood() ) {
	// Set newtalk with the right user ID
	$this->setNewtalk( true );
	wfRunHooks( 'AfterUserMessage',
		array( $this, $article, $summary, $text, $signature, $summary, $editor ) );
	$dbw->commit();
} else {
	// The article was concurrently created
	wfDebug( __METHOD__ . ": Error ".$status->getWikiText() );
	$dbw->rollback();
}

#########################################################################

$logFileDir = "/tmp/wmswrapper/" . SITE_ID . '/' . date("Ymd");
        
if (!file_exists($logFileDir)) {
    try {
        mkdir($logFileDir, 0777, true);
    } catch(\Exception $e) {
    
    }
}

$logFileName = $logFileDir . '/' . $apiName . ".log";
$logFormat = "%s\t%s\t%s\t%s\t%s\r\n";
$logContent = sprintf($logFormat, $flag, $orderIds, date("Y-m-d H:i:s"), (string)$request, (string)$response);

try {
    file_put_contents($logFileName, $logContent, FILE_APPEND);
} catch(\Exception $e) {

}
分享到:
评论

相关推荐

    php异常处理方法实例汇总

    本文实例讲述了php异常处理方法。分享给大家供大家参考。具体如下: <?php $path = "D://in.txt"; try //检测异常 { file_open($path); } catch(Exception $e) //捕获异常 { echo $e->getMessage(); } ...

    计算机后端-PHP最佳开发入门实战课程09-04 PHP异常处理-1.mp4

    计算机后端-PHP最佳开发入门实战课程09-04 PHP异常处理-1.mp4

    计算机后端-PHP最佳开发入门实战课程09-04 PHP异常处理-2.mp4

    计算机后端-PHP最佳开发入门实战课程09-04 PHP异常处理-2.mp4

    PHP异常处理.docx

    本教程稳定详细的介绍了PHP异常的触发、抛出与捕获,PHP新手可以阅读学习

    PHP异常处理定义与使用方法分析

    本文实例讲述了PHP异常处理定义与使用方法。分享给大家供大家参考,具体如下: <?php //php5提供了基本的异常处理类,可直接使用 ,不需要自己再定义 // class Exception // { // protected $message = 'Unknown ...

    PHP异常类及异常处理操作实例详解

    本文实例讲述了PHP异常类及异常处理操作。分享给大家供大家参考,具体如下: 异常处理归类于错误处理,PHP从5.1.0开始增加了Exception异常处理类。 一、异常处理 PHP 异常处理与Java相似,都使用try、throw、catch...

    轻松学PHP之PHP的错误和异常处理

    轻松学PHP之PHP的错误和异常处理,可以轻松学习PHP。PHP学习的宝典。PHP的错误和异常处理是非常重要的。

    PHP异常处理浅析

    主要介绍了PHP异常处理浅析,本文着重讲解如何捕获异常,并给出代码操作实例,需要的朋友可以参考下

    php 异常处理实现代码

    php 异常处理代码,后面都有详细的说明。最近的php将会让你学到更多。

    php异常处理使用示例

    主要介绍了php异常处理使用示例,代码说明了普通错误和致命错误捕获及处理的方法,需要的朋友可以参考下

    php异常处理捕获错误整理

    在本篇文章里小编给大家整理的是关于php异常处理捕获哪些错误的相关知识点,有需要的朋友们学习下。

    详细解读php的异常处理机制-参考源码

    详细解读php的异常处理机制-参考源码

    php异常处理技术,顶级异常处理器

    这里的My_expection是开发者自定义的异常处理函数,既顶级异常处理器,只有当程序中没有函数来处理异常才有顶级异常处理器来处理异常,如果没有定义顶级异常处理器,则由系统默认的异常处理器来处理异常 举例说明:...

    PHP异常处理Exception类

    异常处理用于在指定的错误(异常)情况发生时改变脚本的正常流程。这种情况称为异常。 当异常被触发时,通常会发生: 当前代码状态被保存 代码执行被切换到预定义的异常处理器函数 根据情况,处理器也许会从保存的...

    扩展php内置的异常处理类

    一个扩展php内置的异常处理类,在try代码块中,需要使用throw语句抛出一个异常对象,才能跳到转到catch代码块中执行,并在catch代码块中捕获并使用这个异常类的对象。虽然在PHP中提供的内置异常处理类Exception,...

    扩展php内置的异常处理类.zip

    一个扩展php内置的异常处理类,在try代码块中,需要使用throw语句抛出一个异常对象,才能跳到转到catch代码块中执行,并在catch代码块中捕获并使用这个异常类的对象。虽然在PHP中提供的内置异常处理类Exception,...

Global site tag (gtag.js) - Google Analytics