Forum > Beginners

raising EOutOfMemory if raising failed

(1/2) > >>

Kays:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---myGreatException := EArgumentException.create('u stupid');if not assigned(myGreatException) then  OutOfMemoryError;end;raise myGreatException;instead of just

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var SomeObject:TObject;begintry  SomeObject := TObject.Create;except  On EOutOfMemory do  .....end; 

Kays:

--- Quote from: Thaddy on March 30, 2016, 07:56:17 am ---[…]

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var SomeObject:TObject;begintry  SomeObject := TObject.Create;except  On EOutOfMemory do  .....end; 
--- End quote ---
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---raise EArgumentException.create('u stupid'); would render as
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:

--- Quote from: Thaddy on March 30, 2016, 08:50:56 am ---If 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.
--- End quote ---

Gotcha! Thx.


--- Quote from: Thaddy on March 30, 2016, 08:50:56 am ---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:
--- End quote ---
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.

Navigation

[0] Message Index

[#] Next page

Go to full version