How to find out Error No. in ObjFPC in a try-except statement - pascal

I am trying to find a way to detect the error no in objfpc, what I tried is shown below:
Program ErrorHandling;
{$R+}
{$MODE objfpc}
Uses
SysUtils, crt;
Var
intvar: 1 .. 100;
Begin
Try
clrscr;
writeln( 'enter intvar: ');
readln(intvar);
Except
on
e: Exception
Do
Begin
writeln('In Exception, IOResult: ',IOResult);
Case IOResult Of
201: writeln('Range intvar out of range 1-100!'); {How can we find that Error no is 201}
Else
writeln('Unkown Error!'); readln;
End
End
End;
writeln('intvar: ' , intvar);
readln;
End.
But How can we find if the 201 Range Error occurs. I tried using IOResult command but it always shows "Unknown Error" string of my case statement.

Using exceptions you can check error type by type of exception itself.
The classic way is:
try
clrscr;
writeln('enter intvar: ');
readln(intvar);
except
on e: ERangeError do // Executes when raised exception is ERangeError
begin
Writeln('Range intvar out of range 1-100!');
end;
on e: Exception do // Executes for any other exceptions
begin
Writeln(e.ClassName); // Shows exception class you can use at the "on e:" constuction
Writeln(e.Message);
end;
end;
Look at The try...except statement in the official documentation.

Related

how do I rectify this switch case error in pascal?

This is a pascal program that returns factor . rate is the input which is given by the user.
This program is throwing me error.
please do have a look and help.
I want to rectify the error.
I'm not able to find out the error since im very new to pascal and im trying to learn
program Mss;
var
rate,factor:integer;
begin
readln(rate);
case rate of
1..2:begin
factor:=(2*rate)-1;
writeln(factor);
end
3:begin throws error here
factor:=(3*rate)-1;
writeln(factor);
end
4:begin
factor:=(4*rate)-1;
writeln:=(factor);
end
5:begin
factor:=(3*rate)-1;
writeln(factor);
end
6..8:begin
factor:=rate-2;
writeln(factor);
end
else begin
writeln(rate);
end
end;
This is a switch case which returns factor. rate is the input from user.
this throws me an error.
Fatal: Syntax error, ";" expected but "ordinal const" found
You have a few syntax errors. Your begin/end blocks need to be followed by ;.
writeln:=(factor) should be writeln(factor).
And you need an end. to finish the program.
program Mss;
var
rate,factor:integer;
begin
readln(rate);
case rate of
1..2:begin
factor:=(2*rate)-1;
writeln(factor);
end;
3:begin
factor:=(3*rate)-1;
writeln(factor);
end;
4:begin
factor:=(4*rate)-1;
writeln(factor);
end;
5:begin
factor:=(3*rate)-1;
writeln(factor);
end;
6..8:begin
factor:=rate-2;
writeln(factor);
end;
else begin
writeln(rate);
end
end
end.
Also, please note that ; in Pascal is a separator, so you could write code like:
6..8:begin
factor:=rate-2;
writeln(factor);
end;
As:
6..8:begin
factor:=rate-2;
writeln(factor)
end;

Dynamic array access violation error

program LengthOfArray;
uses sysutils;
procedure Main();
var myArray: array of String;
begin
myArray[0] := 'hi';
myArray[1] := 'bye';
myArray[2] := 'hello';
WriteLn('array is: ', Length(myArray));
end;
begin
Main();
end.
When I run this, it gives me an error like this:
An unhandled exception occurred at $0040210C :
EAccessViolation : Access violation
$0040210C
$00401540
$004015ED
When compiling, it gave me a message Warning: Local variable "myArray" does not seem to be initialized... but I don't understand because I assigned 3 values to my array already. Isn't that initialized?

Pascal Access Violation when calling a variable in a class

I have made some very simple code in Pascal that is getting me this error:
Project BugFixing.exe raised exception class EAccessViolation with message 'Access violation at address 0040F1EE in module 'BugFixing.exe'. Write of address 00000004'.
The program consists of 2 modules:
BugFixing.dpr:
program BugFixing;
{$APPTYPE CONSOLE}
uses
SysUtils, uLinearProgrammingMainLogic in 'uLinearProgrammingMainLogic.pas', math;
var
MinOrMax : integer ;
Question : TQuestion ;
begin
try
Randomize ;
MinOrMax := RandomRange(0,2) ;
Question.SetMaximiseQuestion(MinOrMax);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
And uLinearProgrammingMainLogic.pas:
unit uLinearProgrammingMainLogic;
interface
uses sysUtils, math ;
type
TQuestion = class
private
MaximiseQuestion : boolean ;
public
procedure SetMaximiseQuestion (MinOrMax : integer) ;
end;
implementation
procedure TQuestion.SetMaximiseQuestion(MinOrMax : integer);
begin
if MinOrMax = 0 then
MaximiseQuestion := true
else
MaximiseQuestion := false ;
end;
end.
If anyone could explain to me why this is creating an access violation, that'd be appreciated. Thanks in advance. :)
A class must always be instantiated (TClassType.create) before use. The only exception to that are class/static methods, but you don't declare them that way (and that is not basic usage anyway)

Exception raised when setting caption property in next form by user-defined procedure

There are little bit problem.
I have an exception like this one
"Project project1 raised exception class 'External: SIGSEGV'.
In file '.\include\control.inc' at line 3251:
GetTextMethod := TMethod(#Self.GetTextBuf);
Code, which trigger exception:
procedure load_translation(language: string);
var
iit: integer;
begin
Form1.Caption:='Form 1';
Form2.Caption:='Form 2'; //as well as Unit2.Form2.Caption:='Form 2';
end;
procedure TForm1.Image1Click(Sender: TObject);
begin
language:='en';
load_translation(language);
end; `
I have a second Form, which already designed (not in runtime), so Exception raised when setting Text property of TEdit in custom component (Lazarus) not applicable in my case.
debug output:
<-stack-list-arguments 1 0 0>
^done,stack-args=[frame={level="0",args=[{name="this",value="<error reading variable>"},{name="result",value="0x0"}]}]
(gdb)
<-stack-list-locals 1>
^done,locals=[{name="LEN",value="5987780"},{name="GETTEXTMETHOD",value="{CODE = 0xc, DATA = 0x3676c0}"},{name="GETTEXT",value="0x25cf9d8"},{name="RESULT",value="0x25cf9d8"}]
(gdb)

Get Actual Error Message From Oracle Exception In Dot Net

I have this procedure:
create or replace PROCEDURE MyProc
(
<some-parameters>
)
AS
BEGIN
if(<some-condition>) then
RAISE_APPLICATION_ERROR('my custom error message');
end if;
END;
When calling it from C#:
try
{
<call procedure>
}
catch(OracleException x)
{
lblMessage.Text = x.Message;
}
I am getting error message like:
ORA-28008: my custom error message ORA-06512: at blah, line blah ORA-06512: at line blah
I want only:
my custom error message
There is no innerException. Errors collection don't help. Same situation when use Exception instead of OracleException.
What am I missing?
I can use string manipulation but how fixed is format of error message?
I use return parameters to capture with php. I recon the same technique will be useful in .net.
(I assume it's possible to catch the custom message in the exception handling and re-raising only that custom message, but underneath method works for sure. :)
create or replace PROCEDURE MyProc (p_result out varchar2)
is
...
begin
...
if error then
p_result := 'my custom error message';
return; -- exit procedure
end if;
p_result := 'ok';
end;
Try
PKG_MSG.RAISE_ERROR( 0,null,'my custom error message',null,null,null,null,null,null,null,null,null,null );
instead of
RAISE_APPLICATION_ERROR('my custom error message');

Resources