www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - array index out of bound may not throw exception?

reply mw <mw g.c> writes:
Hi,

I have thought array index out of bound always throw exceptions.

However, I just debugged a case, where out of bound array index 
didn't throw exception, and just hang the thread, which is much 
harder to debug (than exception which tells the exact error and 
source line location).

So my question: array index out of bound may not throw exception 
in D?

I tried DMD and LDC, both have this problem.

Is there any flag I can pass to the compiler to let it always 
throw exception?

Thanks.
Jul 21 2023
next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Friday, July 21, 2023 3:27:45 PM MDT mw via Digitalmars-d-learn wrote:
 Hi,

 I have thought array index out of bound always throw exceptions.

 However, I just debugged a case, where out of bound array index
 didn't throw exception, and just hang the thread, which is much
 harder to debug (than exception which tells the exact error and
 source line location).

 So my question: array index out of bound may not throw exception
 in D?

 I tried DMD and LDC, both have this problem.

 Is there any flag I can pass to the compiler to let it always
 throw exception?

 Thanks.
Well, strictly speaking, it throws a RangeError, which is an Error, not an Exception (they both derive from Throwable). And it's not guaranteed that stuff like scope statements, and catches, and the like get run with Errors. Errors are basically supposed to kill your program rather than be recoverable (though they should normally still be printed out by the runtime like with an Exception). However, it at least used to be the case that all of the stack unwinding was in place for them even if it's not guaranteed by the language. So, I don't know what exactly happens right now if a RangeError gets thrown in a separate thread. It would not entirely surprise me if having the thread hang is normal at the moment, though ideally, what should be happening is that the program as a whole would be killed after printing out the RangeError. Either way, threads certainly complicate the matter. However, ignoring the issue of it happening on a separate thread, whether bounds checking occurs in a piece of code depends on a variety of factors - a key one being whether the code in question is safe or not. If no compiler flags are used, then you should be getting bounds checking in all code. If -release is used, then you should be getting bounds checking in safe code, but it will not happen in system or trusted code. So, if you're not marking code with safe, and it's not in templated or auto functions where safe is inferred, then you won't be getting bounds checking in that code if you use -release. If you use -boundscheck=on, then you should be getting bounds checking in all code. If you use -boundscheck=safeonly, then you should be getting bounds checking in safe code (but only safe code) like with -release. If you use -boundscheck=off, then you shouldn't be getting bounds checking anywhere. So, if I had to guess, you did something like use -release with code that isn't safe, and so bounds checking was turned off, but it's also possible that you ran into some issue with how threads are handled and didn't get info on the RangeError when you should have. Unfortunately, it's been long enough since I dealt with a program that ran into a problem like this on a separate thread that I don't know exactly what the current state of that is. But the first step for you would to be sure of which compiler flags that you're using and whether the code in question is safe. That will tell you whether a RangeError should be being thrown or not when an array index is out-of-bounds. - Jonathan M Davis
Jul 21 2023
prev sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Friday, 21 July 2023 at 21:27:45 UTC, mw wrote:
 However, I just debugged a case, where out of bound array index 
 didn't throw exception, and just hang the thread
Uncaught exceptions in a thread terminate that thread and are reported when you call the `join` method of the thread. I think you can still get a debugger to break on them but im not entirely sure, it is a different switch than in the main thread i think. But you might want to do your own try/catch block around a thread to report it differently if you have a better way for your application.
Jul 21 2023
parent reply mw <m g.c> writes:
On Friday, 21 July 2023 at 23:32:41 UTC, Adam D Ruppe wrote:
 On Friday, 21 July 2023 at 21:27:45 UTC, mw wrote:
 However, I just debugged a case, where out of bound array 
 index didn't throw exception, and just hang the thread
Uncaught exceptions in a thread terminate that thread and are reported when you call the `join` method of the thread.
Is there a way to let it report on the spot when it happens?
Jul 21 2023
next sibling parent Andrew <andrewlalisofficial gmail.com> writes:
On Friday, 21 July 2023 at 23:40:44 UTC, mw wrote:
 Is there a way to let it report on the spot when it happens?
The best way is to wrap your thread's main function in a try-catch block and just print whatever error/exception is caught.
Jul 22 2023
prev sibling parent Kagamin <spam here.lot> writes:
On Friday, 21 July 2023 at 23:40:44 UTC, mw wrote:
 Is there a way to let it report on the spot when it happens?
On linux if you catch an exception and call abort, the debugger will show you where abort was called, on windows you can call DebugBreak function, the debugger will show where it was called.
Jul 27 2023