parameters - In PHP v 5.6 how do I catch UnexpectedValueException?
Get the solution ↓↓↓I've reviewed a number of ways to determine that a directory is empty or not, and many indicate that$isDirEmpty = !(new \FilesystemIterator($dir))->valid();
is a good choice, except that the directory path in the $dir parameter must exist or else anUnexpectedValueException
is thrown. Please see How can I use PHP to check if a directory is empty? for more details about using this method.
However, this and other examples first need to use other functions/statements to determine if the directory exists before using FileSystemIterator to retrieve the iterator and test its item, but these often cache information about the directory and that needs to be explicitly released after they are used, which many of these examples do not do.
To get around these extra 'details' and aid in my debugging, I've decided to implement the FileSystemIterator technique in a function that uses a try/catch block and instead of the validmethod
that returns true if the file system item is valid (whatever that means) or false, I'm using thegetPathname
method so I can see the actual directory/file's path name when I debug the code.
Here is my function, but I need the catch() parameter to finish it:
function isDirectoryEmpty( $dir ) {
//
// Create a file system iterator from the directory path and get the first
// directory content item, if one exists, Note, FilesystemIterator ignores
// the special . and .. entries in a directory that always exist, so I don't
// need to filter them out with code.
try {
$iterator = new \FilesystemIterator( $dir );
foreach( $iterator as $fileinfo ) {
$filePathName = $fileinfo->getPathname();
break;
}
}
catch( WHAT_GOES_HERE ) {
$filePathName = '';
}
return ( $filePathName === '' );
}
Since I only need one existing directory member to prove is the directory isn't empty, I break out of theforeach
block that fetches the file information from the iterator, but it would be cleaner if I knew how to do this without theforeach
loop statement. Would someone also give me the code that does this.
UPDATE
The foreach loop can be replaced as follows:
$filePathName = ''; // Assume that the $dir directory is empty, even if it
// doesn't exist and the FilesystemIterator creation
// method throws an exception.
try {
$filePathName = ( new \FilesystemIterator( $dir ) )->getPathname();
}
catch( UnexpectedValueException $exception ) {} // Other than catching the
// exception, no other action
// is needed.
Thanks.
Answer
Solution:
If the exception that you are getting isUnexpectedValueException
, you can catch it specifying its type in the catch statement like this:
try {
// some logic
} catch( UnexpectedValueException $exception) {
// Print the exception message
echo 'Message: ' .$exception->getMessage();
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: string literal contains an unescaped line break
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.