Recent

Author Topic: [SOLVED] Handling Exception  (Read 12044 times)

Deepaak

  • Sr. Member
  • ****
  • Posts: 445
[SOLVED] Handling Exception
« on: May 28, 2014, 08:28:43 am »
If invalid string is passed to below function it generates a error dialog, how to get rid of this default error dialog with my custom error message/dialog.

Code: [Select]
function EncodeStringBase64(const s:string):String;     
« Last Edit: May 31, 2014, 11:16:53 am by Deepaak »
Holiday season is online now. :-)

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: Handling Exception
« Reply #1 on: May 28, 2014, 08:40:46 am »
Code: [Select]
  try
    EncodeStringBase64(MyString);
  except
    On E :Exception do begin
      ShowMessage(E.Message);
    end;
  end;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Deepaak

  • Sr. Member
  • ****
  • Posts: 445
Re: Handling Exception
« Reply #2 on: May 28, 2014, 08:52:54 am »
Code: [Select]
  try
    EncodeStringBase64(MyString);
  except
    On E :Exception do begin
      ShowMessage(E.Message);
    end;
  end;


Thank you, Worked like a charm, can we use finally in conjunction with except like this

Code: [Select]
Try
   //do some thing
except
   //handle error
finally
   //free the object

And is there any method to get the error code instead of error strings.
« Last Edit: May 28, 2014, 08:55:23 am by Deepaak »
Holiday season is online now. :-)

dogriz

  • Full Member
  • ***
  • Posts: 107
    • Tech blog - Delphi, Lazarus, Firebird, Windows, Linux, Android...
Re: Handling Exception
« Reply #3 on: May 28, 2014, 09:29:17 am »
Code: [Select]
  try
    EncodeStringBase64(MyString);
  except
    On E :Exception do begin
      ShowMessage(E.Message);
    end;
  end;


Thank you, Worked like a charm, can we use finally in conjunction with except like this

Code: [Select]
Try
   //do some thing
except
   //handle error
finally
   //free the object

And is there any method to get the error code instead of error strings.

No, not like that, but you can use it like this:
Code: [Select]
  try
    try
    except
    end;
  finally
  end;
FPC 3.0.4
Lazarus 2.0.6
Debian x86_64, arm

Deepaak

  • Sr. Member
  • ****
  • Posts: 445
Re: Handling Exception
« Reply #4 on: May 28, 2014, 09:52:49 am »
No, not like that, but you can use it like this:
Code: [Select]
  try
    try
    except
    end;
  finally
  end;

How to place the function so both case is executed for example

Try
   Dothis()

   Try
      //
    except
     // handle error
    end;

finally
   //free the function

end;


Holiday season is online now. :-)

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: Handling Exception
« Reply #5 on: May 28, 2014, 11:50:33 am »
Try except end; will catch what ever is designed to catch if you use the generic Exception as in my 1st example then it will catch all the errors and exceptions marking them as handled it doesn't matter what code exists after the except block if every thing is catched  then everything after the except block will be executed. for example
Code: [Select]
try
except
  on E:Exception do begin
  end;
end;
Cleanup;
has the same effect as
Code: [Select]
try
  try
  except
    on E:Exception do begin
    end;
  end;
finally
  Cleanup;
end;

What the second construct guards against is unhandled errors eg
Code: [Select]
try
  try
  except
    on E:EDivByZero do begin
    end;
  end;
finally
  Cleanup;
end;
On this example the only error that is handled on the except block is the division by zero in which case all other errors would have left memory leaks if the code was not guarded with the finally block.

There are no error numbers in pascal (well there are but they are converted to exception classes) you manage the exception classes as shown on the second example you have to be aware that the handling process is hierarchy aware. EG
Code: [Select]
try
except
  on E:EDivByZero  do begin {do your correction} end;
  on E:Exception do begin {OOPS don't know why there is an error}end;
end;
if a Division By Zero occurs then it is handled and the generic "E:Exception" block is not executed for any other error the generic block is executed, now if you change the code to
Code: [Select]
try
except
  on E:Exception do begin {OOPS don't know why there is an error}end;
  on E:EDivByZero  do begin {do your correction} end;
end;
Then the eDivByZero will never be executed as the generic handler will be found first and executed. If you control+Click on the EDivByZero word inside lazarus it should open the file that is declared and jump in the class declaration automatically there you can see that the this exception is based on (or inherits from) the Exception class. You can have multilevel hierarchys eg MyCompaniesException = Class(Exception); MyCompaniesAwesomeSuiteException = class(MyCompaniesException); MyCompaniesMathException= class(MyCompaniesxception); MyDivByZero = class(MyCompaniesMathException); etc you get the picture I guess. Now you can write a generic handler for all MyCompaniesxception using
Code: [Select]
try
except
  on E:MyCompaniesException do begin {OOPS my bad let me see what I can do}end;
  on E:EDivByZero  do begin {do your correction} end;
  // everything else is propagated to the end user.
end;

EDIT:
One last thing what ever is out side the try except/finally commands it is not guarded against the errors eg
Code: [Select]
procedure Test
raiseErro1;
try
  RaiseError2;
  try
    RaiseError3;
  except
    on E:Exception do begin
    end;
  end;
finally
  Cleanup;
end;
end;
raiseError3 will end up in the E:Exception generic handler and then it will execute the cleanup, RaiseError2 will trigger the finally block, execute the cleanup call and then reraise the error exiting the procedure. RaiseError1 will not trigger neither except nor finally blocks and will jump out of the procedure.
« Last Edit: May 28, 2014, 12:13:17 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8141
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Handling Exception
« Reply #6 on: May 29, 2014, 03:32:47 am »
No, not like that, but you can use it like this:
Code: [Select]
  try
    try
    except
    end;
  finally
  end;

How to place the function so both case is executed for example

Try
   Dothis()

   Try
      //
    except
     // handle error
    end;

finally
   //free the function

end;
You already do it. The inner try-except will handle the exception while the outer try-finally will always be executed.

Deepaak

  • Sr. Member
  • ****
  • Posts: 445
Re: Handling Exception
« Reply #7 on: May 31, 2014, 11:16:21 am »
Thank you @taaz  ;D   :)
Holiday season is online now. :-)