IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
   ► KBTo/From GuidesReferenceStatements  Print This     

Exception Trapping (Cross Ref > Statements)

Exception Trapping

Languages Focus

A common usage of exception handling is to obtain and use resources in a "try-it" block, deal with any exceptions in an "exceptions" block, and release the resources in some kind of "final" block which executes whether or not any exceptions are trapped.

ASP Classic:   On Error

Syntax Example:
On Error Resume Next
Response.Write FormatDateTime(f_CurrentActualDate, vbShortDate)
  
  If ErrNumber <> 0 Then
Break(f_CurrentActualDate)
End If
On Error Goto 0

More Info

KB Post:  Using On Error Resume Next

More Info

C#:   try...catch...finally

C# uses a try...catch...finally statement to trap for errors.

try {}
catch {}
finally {}
Syntax Example:
try
{
int y = 0;
y = 1 / y;
}
catch
{
MessageBox.Show("you cannot divide by zero");
}

Complete Simple Example

Here is a complete example using a button click event:

private void button6_Click(object sender, EventArgs e)
{
try
{
MessageBox.Show("Before Error");
int y = 0;
y = 1 / y;
MessageBox.Show("This line never executes: " + y);
}
catch
{
MessageBox.Show("you cannot divide by zero");
}
finally
{
MessageBox.Show("This line always executes.");
}
}

More Info

Code:  C# Exception Trapping (try...catch...finally)

C++:   try/catch

Syntax Example:
try {
  //Some code.
}
catch(AnError) {
  //Error code here.
}

More Info

Corel Paradox:   try...onFail

ObjectPAL has a try...onFail statement but does not have a finally-type component. However, the code afer endTry will execute.

try
onFail
endTry
Syntax Example:
var
i SmallInt
endVar
try
i = 0
i = 1/i
onFail
msgInfo("", "You cannot divide by zero.")
endTry

Here is an example from a pushButton event:

method pushButton(var eventInfo Event)
var
 i SmallInt
endVar
 
errorTrapOnWarnings(True)
 
try
  i = 0
  i = 1/i
  msgInfo("", "This line of code never executes.")
onFail
  msgInfo("", "You cannot divide by zero.")
endTry
 
msgInfo("", "This line always executes.")
endMethod

The following example shows you the last ObjectPAL error:

errorTrapOnWarnings(yes)
try
  ;Your code here
onFail
  msgStop( errorCode(), errorMessage() )
  ;You could also use errorShow()
endTry

More Info

Article:  Debugging ObjectPAL Code

More Info

Delphi:   try..except, try..finally

Use a try..except..end block to trap and process errors.

Delphi also offers a try...finally where code will execute in the finally section no matter what. It's common to put a try..except inside a try..finally.

Syntax Example:
var
y : Double;
begin
try
y := 0;
y := (1/y);
ShowMessage(FloatToStr(y));
except
ShowMessage('You cannot divide by zero.');
end;
end;

More Info

Delphi Prism:   try..except, try..finally

Use a try..except..end block to trap and process errors.

Delphi also offers a try...finally where code will execute in the finally section no matter what. It's common to put a try..except inside a try..finally.

Syntax Example:
try
  var y: Integer;
  y := 0;
  y := 1/y;
except
  MessageBox.Show("You cannot divide by zero.");
end;

More Info

Java:   try/catch/finally

Syntax Example:
try {
  /* Risky code here. */
}
catch (SomeException) {        //one or more.
  /* Recovery here. */
}
finally {                      //0 or one.
  /* Do something. */
}

More Info

JavaScript:   try/catch/finally

See "throw" to raise (throw) an error.

Syntax Example:
try {
  //Do something.
}
catch(e) {        //one or more.
  //Do something.
}
finally {         //0 or one.
  //Do something.
}

More Info

VB.Net:   Try...Catch...Finally

VB.Net uses a try...catch...finally statement to trap for errors.

Try
Catch
Finally
End Try
Syntax Example:
Try
Dim y As Integer = 0
y = 1 / y
Catch
MessageBox.Show("you cannot divide by zero")

End Try

Complete Simple Example

Here is a complete example using a button click event:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  Try
    MessageBox.Show("Before Error")
    Dim y As Integer = 0
    y = 1 / y
    MessageBox.Show("This line never executes: " + y)
  Catch
    MessageBox.Show("you cannot divide by zero")
  Finally
    MessageBox.Show("This line always executes.")
  End Try
End Sub


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Intermediate

1 Intermediate Level Question

Question #1: Multiple Choice

Which of the following syntax templates is used for error handling?

Answer:
1. 
Try
Except
Final
End Try
2. 
Try
OnFail
OnFinal
End Try
3. 
Try
Handle Exception
Do Final
End Try
4. 
Try
Catch
Finally
End Try
5. 
Try
Exception
Cleanup
End Try

More Info

Code:  VB.Net Exception Trapping (Try...Catch...Finally)




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2023 Prestwood IT Solutions.   [Security & Privacy]