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) {

}

你可能感兴趣的:(异常处理)