The saying goes “exceptions are exceptional”. I would say connecting to a database is crucial and without it my program cannot function. Therefore makes sense to throw an exception on failure to connect.
<code>try {
database_class db(path); // will throw on failure
// my whole program end up in here
catch(int e) {
return e;
}
</code>
<code>try {
database_class db(path); // will throw on failure
// my whole program end up in here
catch(int e) {
return e;
}
</code>
try {
database_class db(path); // will throw on failure
// my whole program end up in here
catch(int e) {
return e;
}
As you can see database object is created inside a try-catch block, therefore it is inaccessible outside the block. Which means now entire program has to sit inside try-catch block, otherwise I cannot reach database.
That does not seem right. Is this really how try-catch block is supposed to be used?
2