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???

1
  • 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. Commented Feb 22, 2014 at 7:49

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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?
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
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
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
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.