What is the use of Try? It is allowing an error ?

Post

Posted
Rating:
#1 (In Topic #1290)
Regular
sergioabreu is in the usergroup ‘Regular’
If I use Try, the <COLOR color="#FF0000">error passes by</COLOR>… If i  remove the word Try THEN <COLOR color="#008000">the error is catched</COLOR>.

How to use Try and what is the need if the real catch occurs without Try

Code

Try re = New RegExp(Mix, newPatt, CompOpt, ExecOpt)
     Return re
  Finally
    Message.Error("Fin: Pattern inválido, verifique")
  Catch
     Message.Error("Cat: Pattern inválido, verifique")
End
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
Consider the following code: -

TIP: - To get nice looking code below use the 'gb' button not the '</>' button

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim sText As String
  4.  
  5.   Try sText = File.load(User.Home &/ "MyFile.txt")
  6.     Message.Error("The file is missing!!", "OK")
  7.     Me.Close
  8.   End If
  9.  
  10.   Print sText
  11.  
  12.  

If the file does not exist, a warning is given, otherwise the text in the file is printed.
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
Finally and Catch are part of the Error handling routines.

If you do not use "Try" then the Finally and Catch statements are used as expected.

using Try is for suppressing the normal error management. any error can then be detected yourself by checking "Error" (as Cogier has shown) but an error will not be raised, that's the point of "Try"

it can also be useful for just suppressing errors and can make for faster code. like these 2 statements

Code (gambas)

  1. Try Kill "/tmp/tempfile"  ' just try to delete a file but don't error if it does not exist.
  2.  
  3. If Exist("/tmp/tempfile") Then Kill "/tmp/tempfile"  ' check first if it exists then delete if so.

probably the first one is faster.
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
sergioabreu is in the usergroup ‘Regular’
 Interesting

thanks 2 both guys
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
sergioabreu is in the usergroup ‘Regular’
 In most languages Try means "get the error" (to be treated in catch), in gambas it means "don't bother me".

I DID like it  lol
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
 It appears to me that :    Try and error are for a single statement

and :    Finally and Catch are for a block of code

Or have I interpreted it wrong ?
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
sergioabreu is in the usergroup ‘Regular’
 They are 3 different statements I will let the veterans explain better

What I know:

  Try "tries" to supress the error and you can treat (If you want) checking state of Error. Useful to ignore "not important" errors.

  Catch will only work if there is not a try above (tell me if I am wrong)

  Finally must be used before catch because it can raise another error
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
The difference between Finally and Catch is that the code under Catch is run only if there is an error (and you didn't use Try). The code under Finally is run if there is an error or if not an error and you have not exited the function using Return

If Finally is used it must be used before Catch
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
thatbruce is in the usergroup ‘Regular’

sergioabreu said


  Catch will only work if there is not a try above (tell me if I am wrong)


Not exactly true. Catch will work if an Error is detected on any line that is not "protected" by a Try.

Online now: No Back to the top

Post

Posted
Rating:
#10
Regular
sergioabreu is in the usergroup ‘Regular’
It results the same:

Having
a Try protecting the code that would produce the error
, the Catch in a line below below will not catch THAT ERROR

Code

Try   line that could generate error
Catch do not catch
That's what I meant. A Try just above
Online now: No Back to the top

Post

Posted
Rating:
#11
Regular
sergioabreu is in the usergroup ‘Regular’

thatbruce said

sergioabreu said


  Catch will only work if there is not a try above (tell me if I am wrong)


Not exactly true. Catch will work if an Error is detected on any line that is not "protected" by a Try.

"That is ABOVE the Catch line"
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 An error detected in the Catch block "propagates" up through the call stack looking for another Catch (until it finally cr*ps out and aborts the program).
Unless…
There is a top level error handler, I can't remember how it is used?

Online now: No Back to the top

Post

Posted
Rating:
#13
Avatar
Regular
thatbruce is in the usergroup ‘Regular’

sergioabreu said

It results the same:

Having
a Try protecting the code that would produce the error
, the Catch in a line below below will not catch THAT ERROR

Code

Try   line that could generate error
Catch do not catch
That's what I meant. A Try just above

Exactly! THAT line is "protected" which means the interpreter ignores any error that occurs when it is executed. Perhaps you are thinking that TRY is working as a block definition. It isn't, it applies to a single line of code. Similarly CATCH and FINALLY aren't blocks either, they are sort of goto targets. If an error is detected in a non-TRY line and there is a CATCH "label" then the interpreter does a jump to the first line in after the CATCH label.
IOW if an error occurs in an unprotected line then execution immediately jumps to the CATCH, nothing in between is executed.

Theoretically, when executing the CATCH code reaches its end, execution should then jump to the FINALLY and execute that code and then RETURN. But I have doubts about that.
<COLOR color="#FF0000">No, that's wrong. The FINALLY chunk is executed before the CATCH chunk.</COLOR>

Online now: No Back to the top

Post

Posted
Rating:
#14
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
and another thing..
TRY doesn't always do things the way you may think. It depends on the command you use it on.
For example

Code (gambas)

  1. Try Debug <SomeNullObject>.<PropertyName>
can result in weird outcomes.   :?

Online now: No Back to the top
1 guest and 0 members have just viewed this.