raising EOutOfMemory if raising failed lazarus / 61-Beginners ---------------------------------------------------------------------- Kays · 2016-03-30 05:34 Do I have to raise an EOutOfMemory exception if creating an exception (object) for another (= not out of memory) exception (the circumstance) failed, because we're out of memory? So my code would read Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i myGreatException := EArgumentException.create('u stupid');if not assigned(myGreatException) then  OutOfMemoryError;end;raise myGreatException;instead of just Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i raise EArgumentException.create('u stupid'); Also in today's „why the heck?“: EOutOfMemory tells us, there'll be an EOutOfMemory instance reserved, just in case, but no word about its usage. WTH? A search engine search later I found the OutOfMemory procedure, but should be cross referenced from both pages, shan't it? ---------------------------------------------------------------------- Thaddy · 2016-03-30 05:56 You do not normally raise an out of memory exception yourself. Indeed, the out of memory exception is pre-allocated, so that the resources needed to handle the exception are always there. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var SomeObject:TObject;begintry  SomeObject := TObject.Create;except  On EOutOfMemory do  .....end; ---------------------------------------------------------------------- Kays · 2016-03-30 06:13 Quote from: Thaddy on March 30, 2016, 07:56:17 am[…] Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var SomeObject:TObject;begintry  SomeObject := TObject.Create;except  On EOutOfMemory do  .....end; Not what I was hitting for. Let me try to elaborate it even more: My doSomething() got supplied a nil-pointer. That's a bad argument. Thus I wanna raise an EArgumentException. But meanwhile we may also have run out of memory. So the creation of EArgumentException has failed. Now, just writing Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i raise EArgumentException.create('u stupid'); would render as Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i raise nil, eh? Do I have to catch this condition, don't I? Background: I'm utilizing a library written in C. SomeFunction() sets errno ENOMEM. I'd like to have that error number as an exception rather than fpGetErrno() all the time by myself. ---------------------------------------------------------------------- Thaddy · 2016-03-30 06:50 If you run really out of memory, the EOutOfMemory exception will always be raised anyway, so I don't see the point. A possible Raise nil will be preceded by the EOutOfMemory which needs to be handled first. That would mean code like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     var     SomeObject:TObject;    begin    try      SomeObject := TObject.Create;    except      On EOutOfMemory do      .....;      On EArgumentException do      .....;    end;      to handle both. I can imagine you create a special exception object to handle the C library out of memory exception, as in the case of insufficient memory to perform its operation, something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  EExternalInsufficientMemory = class(Exception);var rslt:integer;begin  rslt := PerformSomeFunctionFromDll;  if rslt := ErrNoMem then Raise EExternalInsufficientMemory.Create('Not enough memory to perform operation');end.  But if that fails because there really is no memory, the pre-allocated EOutOfMemory exception will still be raised. You don't have to raise it yourself. ---------------------------------------------------------------------- Kays · 2016-03-30 07:05 Quote from: Thaddy on March 30, 2016, 08:50:56 amIf you run really out of memory, the EOutOfMemory exception will always be raised anyway, […] […] if that [raising/creating an exception] fails because there really is no memory, the pre-allocated EOutOfMemory exception will still be raised. Gotcha! Thx. Quote from: Thaddy on March 30, 2016, 08:50:56 amI can imagine you create a special exception object to handle the C library out of memory exception, as in the case of insufficient memory to perform its operation, something like:Does there anything speak against of just utilizing the EOutOfMemory/OutOfMemoryError provided by sysutils? There won't be left many options anyway, so I don't care if the exception came from libFoo or from anywhere else. ---------------------------------------------------------------------- Thaddy · 2016-03-30 07:12 Well, in the general case of raising an exception you would write:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i raise ESomeException.Create But that is not supposed to be done in the special case of the EOutOfMemory exception. That is already created. If you want a special purpose exception, create a special purpose exception and leave the EOutOfMemory exception alone. It is a special case, with special handling and should not be raised by hand. You CAN raise it yourself, but that would be bad practice. In general it gives you just enough stability to end an application gracefully because it should be considered fatal in most cases.