Permalink
Browse files

added exception handling to csviterator.

  • Loading branch information...
1 parent 9f5ed4a commit e8dfd9c74b80573f0a81af910076be2defc3189b @pmeth committed Feb 27, 2012
Showing with 24 additions and 11 deletions.
  1. +8 −0 library/Pmeth/Common/CSVIterator.php
  2. +16 −11 readcsv.php
@@ -15,6 +15,14 @@ class CSVIterator implements \Iterator {
private $headings = array();
public function __construct($file, $withheadings = true, $delimiter = ',') {
+ if(!file_exists($file)) {
+ throw new \Exception('The file ' . $file . ' does not exist');
+ }
+
+ if(!is_readable($file)) {
+ throw new \Exception('The file ' . $file . ' is not readable');
+ }
+
$this->filePointer = fopen($file, 'r');
$this->delimiter = $delimiter;
$this->withheadings = $withheadings;
View
@@ -1,22 +1,27 @@
<?php
+
namespace Pmeth\RBI;
+
include('bootstrap.php');
/*
-// Option 1
-//$csv = new parseCSV('../uploads/sample2.txt', null, null, 'Contact First Name = PETER AND Priority = 0001');
+ // Option 1
+ //$csv = new parseCSV('../uploads/sample2.txt', null, null, 'Contact First Name = PETER AND Priority = 0001');
-$csv = new parseCSV('../uploads/sample2.txt');
+ $csv = new parseCSV('../uploads/sample2.txt');
-foreach($csv->data as $line) {
- print_r($line);
-}
-*/
+ foreach($csv->data as $line) {
+ print_r($line);
+ }
+ */
// Option 2
-$csv = new \Pmeth\Common\CSVIterator('../uploads/sample2.txt');
+try {
+ $csv = new \Pmeth\Common\CSVIterator('../uploads/sample2.txt');
-foreach($csv as $line) {
- print_r($line);
+ foreach ($csv as $line) {
+ print_r($line);
+ }
+} catch (\Exception $e) {
+ die('A problem occurred opening the file. Message: ' . $e->getMessage());
}
-

0 comments on commit e8dfd9c

Please sign in to comment.