digitalmars.D.learn - Why I can't catch the exception?
- Suliman (41/41) Jun 05 2016 I really can't understand why try-catch block do not handle
- Adam D. Ruppe (5/11) Jun 05 2016 You are catching Exception, but it is throwing Error. They are
- Era Scarecrow (9/22) Jun 05 2016 The assertion is being thrown in the storage.d and backtracking
- Piotrek (5/13) Jun 05 2016 You are very close. This is just a limitation of the current
- ag0aep6g (15/31) Jun 05 2016 An AssertError is not an Exception (in the narrow sense) [1], but an
I really can't understand why try-catch block do not handle exception. digit 1 is printing, so exception is accrue after it, but why nothing in catch block? http://img.ctrlv.in/img/16/06/05/57546861d8e81.png Here is my code: void dbSetup() { try { //getcwd do not return correct path if run from task shoulder string dbpath = buildPath((thisExePath[0..((thisExePath.lastIndexOf("\\"))+1)]), dbname); if(!dbpath.exists) { writeln("It's seems you are runnining Application first time\n You should set up admin password"); auto db = DataBase(dbname); writeln("1"); auto usersCollection = db.collection!User("Users", true); // base on struct User usersCollection.put(User(0, "admin", "123", "admins")); // defaults writeln("2"); writeln("[INFO] db with default credentials created"); } else { writeln("[INFO] db exists"); return; } } catch(Exception e) { writeln("Can't setup DB"); writeln(e.msg); } } I am using https://gitlab.com/PiotrekDlang/DraftLib/
Jun 05 2016
On Sunday, 5 June 2016 at 18:02:12 UTC, Suliman wrote:I really can't understand why try-catch block do not handle exception. digit 1 is printing, so exception is accrue after it, but why nothing in catch block? http://img.ctrlv.in/img/16/06/05/57546861d8e81.pngcatch(Exception e)You are catching Exception, but it is throwing Error. They are two separate things. You shouldn't typically catch Error, instead try to fix the bug it indicates. But you can if you want by catch(Error e)
Jun 05 2016
On Sunday, 5 June 2016 at 18:15:47 UTC, Adam D. Ruppe wrote:On Sunday, 5 June 2016 at 18:02:12 UTC, Suliman wrote:The assertion is being thrown in the storage.d and backtracking it basically points to line 115 (usersCollection), so am going to guess based on error messages alone that you are passing a struct/class that doesn't match inputs that it is expecting for one of the elements it needs to store. So my advice is to look at the User struct/class, and then look at the DB's User table. But this is just a far thrown guess at the problem.I really can't understand why try-catch block do not handle exception. digit 1 is printing, so exception is accrue after it, but why nothing in catch block? http://img.ctrlv.in/img/16/06/05/57546861d8e81.png catch(Exception e)You are catching Exception, but it is throwing Error. They are two separate things. You shouldn't typically catch Error, instead try to fix the bug it indicates. But you can if you want by catch(Error e)
Jun 05 2016
On Sunday, 5 June 2016 at 18:20:12 UTC, Era Scarecrow wrote:The assertion is being thrown in the storage.d and backtracking it basically points to line 115 (usersCollection), so am going to guess based on error messages alone that you are passing a struct/class that doesn't match inputs that it is expecting for one of the elements it needs to store. So my advice is to look at the User struct/class, and then look at the DB's User table. But this is just a far thrown guess at the problem.You are very close. This is just a limitation of the current database code which can only handle the simplest structs. https://gitlab.com/PiotrekDlang/DraftLib/issues/4 Piotrek
Jun 05 2016
On 06/05/2016 08:02 PM, Suliman wrote:I really can't understand why try-catch block do not handle exception. digit 1 is printing, so exception is accrue after it, but why nothing in catch block? http://img.ctrlv.in/img/16/06/05/57546861d8e81.png Here is my code: void dbSetup() { try {[...]} catch(Exception e) { writeln("Can't setup DB"); writeln(e.msg); } }An AssertError is not an Exception (in the narrow sense) [1], but an Error. Both Exception and Error [2] derive from Throwable [3]. An Error signals an unrecoverable problem. You should not try to catch and handle them. An AssertError in particular is thrown when an `assert` fails. In your case the assert is in database\storage.d, line 312. A failing assert signals means that there's an error in the program. A condition is not met that should be met at all times. The compiler will let you catch Error and Throwable, but you really shouldn't do that. Instead fix your code so that it doesn't fail the assert.
Jun 05 2016