PhpSpreadsheet/docs/ReadingSpreadsheetFiles/06-Error-Handling.md

21 lines
850 B
Markdown
Raw Normal View History

2016-11-27 15:51:44 +00:00
# PhpSpreadsheet User Documentation Reading Spreadsheet Files
2013-05-20 11:21:56 +00:00
## Error Handling
2016-11-27 15:51:44 +00:00
Of course, you should always apply some error handling to your scripts as well. PhpSpreadsheet throws exceptions, so you can wrap all your code that accesses the library methods within Try/Catch blocks to trap for any problems that are encountered, and deal with them in an appropriate manner.
2013-05-20 11:21:56 +00:00
2016-11-27 15:51:44 +00:00
The PhpSpreadsheet Readers throw a \PhpOffice\PhpSpreadsheet\Reader\Exception.
2013-05-20 11:21:56 +00:00
```php
$inputFileName = './sampleData/example-1.xls';
try {
2016-11-27 15:51:44 +00:00
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
} catch(\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
2013-05-20 11:21:56 +00:00
die('Error loading file: '.$e->getMessage());
}
```
> See Examples/Reader/exampleReader16.php for a working example of this code.