0

I'm trying to load an excel file to be read with PHPExcel reader object:

$inputFileName = $_FILES['excelimportfile']['tmp_name'];
 //Read your Excel workbook
     try {
           $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
           $objReader = PHPExcel_IOFactory::createReader($inputFileType);
           $objPHPExcel = $objReader->load($inputFileName);
         }catch(Exception $e) {
            $this->session->set_flashdata(
              'error','Error loading file "'.
               pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()
            );
            redirect('admin/zipcode');
         }

But who's gonna throw the exception? this $objPHPExcel = $objReader->load($inputFileName); line should have been used to generate the exception i think. eg:

   $objPHPExcel = $objReader->load($inputFileName);
   if(!$objPHPExcel) throw new Exception($objPHPExcel->load_error(),1);

But I found nothing like this anywhere. What to do now???

edam
  • 910
  • 10
  • 29
  • Theoretically, any one of those three functions inside your `try { ... }` block can throw an exception... Same goes for functions called inside *those* functions, it's turtles all the way down. Besides, when an exception is thrown you get its full stack, including the specific file/line that threw it. – DCoder Feb 22 '14 at 07:49

1 Answers1

1

Code within the PHPExcel library itself throws exceptions rather than returns false as an error status as your last comment seems to suggest that it should; and which suggests that you don't really understand how exceptions work or what their purpose is.

The exceptions that PHPExcel throw from anywhere within the library can be caught and handled by your code, no matter where in the library they are thrown from

There's a whole host of logic in the load() method that can throw an exception (malformed files being one example), but also logic in identify() that can throw an exception too (if the specified file doesn't exist, or can't be read, then the identify() method will throw an exception)....

but it's difficult to understand exactly what you're asking. If you look at the code for the readers, the methods within those classes that can throw an exception are all documented in the phpdoc blocks, and you can see in the class code where exceptions are actually thrown.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Showing all the respect to your reputation and knowing u were one of the authors of PHPExcel I could say that my asking here was to make it clear how PHPEx readers' exception will be caught.. At least there has to be some thrower CAUSE we all know without throw no catch is of work. SO if the last or middle or first line generates exception will it be thrown automatically to my catch block? How is that Sir? – edam Feb 22 '14 at 12:45
  • 1
    If any exception is thrown by either the identify(), createReader() or load() methods, or any methods called by those methods no matter how deep within the call tree, they'll be caught by the catch block of your first example.... that's how Exceptions work in PHP – Mark Baker Feb 22 '14 at 12:47
  • which means that if there were 4-level deep try catch before mine.. All those catches have to throw it so that the last catch in the line gets it(correct me if I were wrong). And I disagree what u said about PHP's exception handling. You could try this on your shell: **try{$x=strtotime("xyz"); var_dump($x);} catch(Exception $e){ echo "Hi";}** According to you.. It shoud have printed "Hi" . But see what does it prints. Regards – edam Feb 22 '14 at 16:35
  • 1
    But there aren't 4 levels of catches in PHPExcel: with a couple of minor exceptions, PHPExcel throws an exception for your script to handle the catching... and strtotime() doesn't throw Exceptions at all, so wrapping strtotime() in a try/catch block is meaningless... and Exception is not the same as an error unless you've changed your error handling to throw exceptions instead; which you can do, but it isn't the default PHP behaviour – Mark Baker Feb 22 '14 at 16:39
  • 1
    The whole point of exceptions is that they __don't__ have to be handled at every level of the call stack, they simply bubble back up the call stack to the first catch – Mark Baker Feb 22 '14 at 16:40
  • Don't u think **Exception is not the same as an error unless you've changed your error handling to throw exceptions instead;** this contradicts with **they'll be caught by the catch block of your first example.... that's how Exceptions work in PHP** ----Sir I think then you should only talk about ImageWorkshop;Not universally about PHP exceptions(Please take no offense..I apologize..). I think no exceptions will ever be caught unless it's thrown. In Our PHPEx reader if it throws exception the exception message has to be carried to the catch block.. How does it happen? Could u explain? – edam Feb 22 '14 at 17:37
  • @edam: *no exceptions will ever be caught unless it's thrown* - isn't that part self-evident? *If* an exception is thrown (whether by your own code or by PHP internals), it will be caught by the first `try/catch` block that wraps the throwing line (from where it can be re-thrown if needed). If the code being executed doesn't throw exceptions and instead relies on error statuses, `try/catch` obviously won't do much. This is covered in the [PHP manual section on Exceptions](http://us.php.net/manual/en/language.exceptions.php) and has very little to do with PHPExcel specifically. – DCoder Feb 22 '14 at 21:22
  • My apologies for being so stupid Mr Edam, I just don't understand what you're saying: all I know is that I programmed PHPExcel to throw PHP Exceptions when it hit an exceptional circumstance that it couldn't otherwise handle, and that it then relies on your script to catch that error and deal with the problem – Mark Baker Feb 22 '14 at 22:28
  • @MarkBaker I too am really sorry for stretching this discussion this far. Well I am a user of something that you made.. Actually your saying "YOU dont understand PHP Exception" got me fanatic on the discussion. Anyway I've all the respect preserved for you cause I believe Out there every programmer is my teacher. – edam Feb 23 '14 at 03:35