Handling exception thrown when font path not set in PhpSpreadsheet - php

I am using PhpSpreadsheet for a project and am trying to use the exact method for setting column widths. It works fine until the true type font path is not set or cannot be found.
The code I have is:
if (EXACT_AUTOSIZE)
{
if (PHP_OS == 'WINNT')
{
\PhpOffice\PhpSpreadsheet\Shared\Font::setTrueTypeFontPath("C:/Windows/Fonts/");
}
else
{
\PhpOffice\PhpSpreadsheet\Shared\Font::setTrueTypeFontPath(__DIR__."/fonts/");
}
try {
\PhpOffice\PhpSpreadsheet\Shared\Font::setAutoSizeMethod(\PhpOffice\PhpSpreadsheet\Shared\Font::AUTOSIZE_METHOD_EXACT);
}
catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
die("Unable to use exact autosize.");
}
}
When executing the script I am getting the error: Uncaught exception 'PhpOffice\PhpSpreadsheet\Exception' with message 'Valid directory to TrueType Font files not specified'
If I change the code to throw the exception in the try block without the function call it is caught by the catch block.

Related

why does php doesn't go inside catch block without throwing an exception

I am new to php, i am from java background, i am wondering why php doesn't goes directly when exception occured in try block without throwing that exception manually.
e.g.
<?php
//create function with an exception
function checkNum($number) {
if($number/0) {
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception in a "try" block
try {
checkNum(2);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below';
}
//catch exception
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
?>
in above example in if condition the divide by zero exception is occured and then it will directly go to the catch block instead it goes to inside if.why?
The code you posted doesn't do what you say it does.
When you execute:
if ($number/0)
the division by zero prints a warning, and then returns false. Since the value is not truthy, it doesn't go into the if block, so it doesn't execute the throw statement. The function then returns true. Since the exception wasn't thrown, the statement after the call to checkNum(2) is executed, so it prints the message.
When I run your code, I get the output:
Warning: Division by zero in scriptname.php on line 5
If you see this, the number is 1 or below
PHP doesn't use exceptions for its built-in error checks. It simply displays or logs the error, and if it's a fatal error it stops the script.
This has been changed in PHP 7, though. It now reports errors by throwing an exception of type Error. This isn't a subclass of Exception, so it won't be caught if you use catch (Exception $e), you'll need to use catch (Error $e). See Errors in PHP 7. So in PHP 7 you could write:
<?php
//create function with an exception
function checkNum($number) {
if($number/0) {
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception in a "try" block
try {
checkNum(2);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below';
}
//catch exception
catch(Error $e) {
echo 'Message: ' .$e->getMessage();
}

Laravel 5.3 Intervention/image NotReadableException using images from urls

How do I deal with the following error so that my script doesn't stop working when the exception occurs:
NotReadableException in AbstractDecoder.php line 302: Image source not
readable
I've tried using the following ($file is the url of the image):
// Return false if error
try
{
$img = Image::make($file);
}
catch(NotReadableException $e)
{
return false;
}
This doesn't seem to catch the exception and return false. What else can I do?
You either need the full namespaced exception in the catch area or add the use statement for that exception at the top of the file
Add Intervention\Image\Exception\NotReadableException:
use Intervention\Image\Exception\NotReadableException;
try {
//
} catch(NotReadableException $e) {
//
}

How to catch an exception from another class method PHP

I'm having trouble catching an exception in PHP
Here's my code.
try {
require $this->get_file_name($action);
}
catch (Exception $e) {
//do something//
}
and the method being called
private function get_file_name($action) {
$file = '../private/actions/actions_'.$this->group.'.php';
if (file_exists($file) === false) {
throw new Exception('The file for this '.$action.' was not found.');
}
else {
return $file;
}
}
Resulting in:
Fatal error: Uncaught exception 'Exception' with message $action was not found.'
Exception: The file for this $action was not found.
However If I put a try-catch block inside of the function and call the function, I'm able to catch the exception no problem.
What am I doing wrong?
If you are catching the Exception inside a namespace, make sure that you fall back to the global namespace:
...
}(catch \Exception $e) {
...
}...
You can also have a look at the following resources:
Why isn't my Exception being caught by catch?
http://php.net/manual/en/language.exceptions.php, top note by user zmunoz
I can't see all of the class body but If You want to use method out of the class it should be Public not Private.
Try to check if file You trying to get exists:
var_dump($file = '../private/actions/actions_'.$this->group.'.php');
IMO there is no file in this path

fatal error: Uncaught exception 'DOMPDF_Exception' with message 'Box property calculation requires containing block width'

When I try to process url http://www.bbc.co.uk/ dompdf throws error
fatal error: Uncaught exception 'DOMPDF_Exception' with message 'Box property calculation requires containing block width' in www\dompdf\include\block_frame_reflower.cls.php on line 171
It seems some settings or some bug?
DOMPDF_Exception is an extension of the Exception class. I'm not sure what parameters it will spit out, but you can dump the array out to see what is being returned:
try{
}catch(DOMPDF_Exception $e){
echo '<pre>',print_r($e),'</pre>';
}
Also found this on Google Code that might help you narrow down, what the actual issue is: http://code.google.com/p/dompdf/issues/detail?id=244
No, you just have to catch exceptions or set your stuff properly.
try {
//Do your stuff here
} catch (Exception $e){
echo $e->message() ;
}
I believe the correct way to return the message is
try {
//Do your stuff here
} catch (Exception $e){
echo $e->getMessage() ;
}

Continue the execution code after a Fatal Error

I use imagick to create thumbnails of PDF files, but in some cases imagic returns a Fatal Error.
I am looking for a way to know when the fatal error occurs.
Something like this:
function MakeThumb($source) {
if($im = new imagick($source)) {
//Generate thumbnail
return($thumb);
} else {
return 'no_thumb.png'; // then we will not tray again later.
}
}
You could do something like this
function MakeThumb($source) {
try {
//throw exception if can't create file
if(!$im = new imagick($source) {
throw new Exception('Count not create thumb');
}
//ok if got here
return($thumb);
} catch (Exception $e) {
return 'no_thumb.png';
}
}
I haven't tested it,but by using Try Catch, you can make it work
http://php.net/manual/en/language.exceptions.php

Categories