Throwable is the base interface for any deployable object.
Source: PHP - Throwable
Exception is the base class for all Exceptions in the PHP 5, and the base class of all user exceptions in the PHP 7.
Source: PHP - Exception
In short, to capture the interface is equivalent to capturing all classes that implement it...
- I don’t know what to call them
Imagine the following scenario: you extends the class \Exception
in your project and, when you want to stop something from running and simply display the message, launch that class. But in the event of a fatal error, literal exception, and so on, you display the error message and complete the execution of the script. Example:
<?php
try {
bar();
if (!baz())
throw new MinhaExcecao('A função baz() retornou falso.');
} catch (MinhaExcecao $e) {
echo $e -> getMessage();
// O script vai continuar a ser executado...
} catch (PDOException $e) {
die('PDO lançou uma exceção. Há algo errado na conexão/consulta do banco de dados.');
// O script não vai continuar a ser executado...
} catch (Throwable $e) {
die('Capturado alguma classe que implementa a interface Throwable');
// O script não vai continuar a ser executado...
}
echo 'foo';
If the function baz()
return false, will fall on the block catch (MinhaExcecao $e)
with the echo
and soon the script will continue on echo 'foo';
... In other cases, the execution will be totally interrupted by the function call die()
.
It is worth remembering that the catch
act as a cascade. When launching some object, it will be captured in the first block catch
compatible with the launched class/interface. This way, your custom class should be captured first or before your parent class/interface, or else it won’t arrive at the catch
intended.
There’s still the block Finally which, theoretically, ALWAYS runs.
Extend the class \Exception
is very useful when you want to pass some error message to the user to which he did something wrong. The other messages are usually errors of the programmer or some feature that is not working properly. Hence it is better to generate a log accessible only to you (admin/dev). The user does not need to know why or where such errors occur. Logically it would be very useful to say something like: "This appeal made an error", but without detailing...
Another typical example is the connection to the database with PHP via PDO that explicitly displays the data when it is not in a block try
/ catch
suitable:
I used the PHP as a reference, but the theory is equivalent in the main programming languages. Perhaps there are some small differences...
Enter in the topic of Specific use of Try-catch and I found nothing talking about Error, in the end I ended up finding other topics about which more or less solved my question, but honestly I don’t think my question is duplicate of this that you marked the doubts are quite different.
– Eduardo Mior
What programming language are you talking about? There is one more than one language that has
Exception
andThrowable
, but the rules vary from one to the other.– Victor Stafusa
I work with java, but I thought the rules of explanation were the same for most other languages.
– Eduardo Mior
From what I understood so much the
Error
as toException
implement the interfaceThrowable
so if I did onecatch (Throwable e)
I will capture all the playable objects that in this case are the exceptions and errors, entertaining if Icatch (Exception e)
I will capture all the flitable objects of the typeException
and if I docatch (Error e)
I will capture all the flitable objects of the typeError
. This right the logic?– Eduardo Mior
In Java,
Throwable
is a class, not an interface. You got a response about PHP because you didn’t say what language you were talking about and a friend there thought it might be PHP. It also meant that your question was confused and poorly elaborated, and therefore closed (after all, it was unclear what language you were talking about). Now, knowing that her question was about Java, she may be a duplicate of this other question.– Victor Stafusa