• No results found

Exception handling

© Copyri ght 200 7 - 2009 ABB . All ri ghts res erved.

4.5. Exception handling

Overview

The .NET programming languages provide built-in support for exception handling, which allows the program to detect and recover from errors during execution.

In managed code, execution cannot continue in the same thread after an unhandled exception. The thread terminates, and if it is the program thread, the program itself terminates. To avoid this, accurate exception handling should be used.

Try-catch-finally

Exceptions are handled in try - catch (-finally ) blocks, which execute outside the normal flow of control.

The try block wraps one or several statements to be executed. If an exception occurs within this block, execution jumps to the catch block, which handles the exception.

The finally block is executed when the Try block is exited, no matter if an exception has occurred and been handled. It is used to clean up system or controller resources.

If you do not know what exceptions to expect or how to handle them, you can catch them and do nothing. This, however, may result in difficult error tracing, as exceptions include information on what caused the problem. Therefore, try at least to display the exception message, either by using a message box or the types Debug or Trace. See Debug output on page 279 and Trace and Debug on page 281 for further information.

Typecasting

When typecasting Signal or RapidData values, for example, there is a potential risk of typecast exceptions. To avoid this you can check the object using the is operator for both value and reference types:

VB:

If TypeOf ARapidData.Value Is Num Then

Dim ANum As Num = DirectCast(ARapidData.Value, Num) ...

C#:

if (aRapidData.Value is Num) {

Num aNum = (Num) aRapidData.Value; ....

}

In C# it is also possible to use the as operator for reference types. A null value is returned if the type is not the expected one:

C#:

DigitalSignal di = this.aController.IOSystem.GetSignal(“UserSig”) as DigitalSignal;

if (di == null) {

MessageBox.Show(this, null, “Wrong type”); }

4.5. Exception handling © Copyri ght 200 7 - 2009 ABB . All ri ghts res erved.

Exception handling for the PC platform

Exceptions thrown from the controller are handled by the PC SDK ExceptionManager, which converts the internal HRESULT to a .NET exception with a reasonable exception description, before it is thrown to the custom application layer. The application handling of these exceptions should apply to general .NET rules.

Exceptions are expensive in a performance perspective and should be avoided if there are other alternatives. If possible use a try-finally block to clean up system and unmanaged resource allocations.

Exception handling for the FlexPendant platform

The FlexPendant SDK provides several exception classes, which are used when errors occur. If the operation is rejected by the controller safety access restriction mechanism, for example, a RejectException is thrown.

If an unhandled exception occurs the application crashes and TAF displays a gray message on the FlexPendant. After confirmation the FlexPendant will restart. To avoid this you should make sure that any potential error situations are dealt with properly by your code.

5.5_1

The message that you get when an unhandled error situation occurs may look like this. Do NOT contact ABB, but fix the error handling of your application.

Continued

4.5. Exception handling © Copyri ght 200 7 - 2009 ABB . All ri ghts res erved. NOTE!

Learn how the exception classes of the FlexPendant SDK work by using the FlexPendant SDK Reference Documentation. Also see SDK exception classes on page 194 to get more detailed information about exception handling for the FlexPendant platform.

.NET Best Practices

.The .NET Framework Developer's Guide presents the following best practices for exception handling:

• Know when to set up a try/catch block. For example, it may be a better idea to programmatically check for a condition that is likely to occur without using exception handling. For errors which occur routinely this is recommended, as exceptions take longer to handle.

• Use exception handling to catch unexpected errors. If the event is truly exceptional and is an error (such as an unexpected end-of-file), exception handling is the better choice as less code is executed in the normal case.Always order exceptions in catch blocks from the most specific to the least specific. This technique handles the specific exception before it is passed to a more general catch block.

4.6. How to use the online help © Copyri ght 200 7 - 2009 ABB . All ri ghts res erved.

4.6. How to use the online help

Overview

The online help comes with the installation of Robot Application Builder and is accessible from Windows Start menu.

The recommendation is to read this user’s guide carefully as you develop your first RAB application. FP SDK Reference and PC SDK Reference are important complements to this manual, as these make up the complete reference to the class libraries of RAB. See

Documentation and help on page 16 for details.

NOTE!

The SDK Reference is NOT integrated in Visual Studio. You must open it from the Start menu.

TIP!

See Documentation and help on page 16 for the web address to RobotStudio Community, where RAB developers discuss software problems and solutions online.