Exceptions
Overview
Robot Raconteur uses exceptions to handle errors. Robot Raconteur has a built-in set of exceptions defined
by the library, and also allows custom exceptions to be defined in service definitions.
See the Framework Exceptions Documentation
for a full list of exceptions, and the documentation for each programming language for how they are used.
All custom exceptions extend RobotRaconteurRemoteException
.
Robot Raconteur can transparently pass exceptions from the service to the client (or client to service for callbacks).
The following operations will transparently pass exceptions:
* property
get and set
* function
calls
* objref
get
* pipe
connect and close endpoints (for client)
* callback
calls (passing from client to service)
* wire
connect, close (for clients), peek, and poke
* memory
all operations
The example service definition experimental.exception_example
contains a custom exception and an object
with functions that will throw exceptions as an example. The service definition is shown below:
1service experimental.exception_example
2
3stdver 0.10
4
5exception MyExampleCustomException
6
7object ExceptionExample
8 function void my_exception_function1()
9 function void my_exception_function2()
10end
The following example code demonstrates how to catch exceptions thrown by the example service:
1# exception_example.py - Example of using exceptions in Python
2
3from RobotRaconteur.Client import *
4
5# Connect to the service
6url = 'rr+tcp://localhost:53224/?service=exception_example'
7c = RRN.ConnectService(url)
8
9# Call the functions
10
11# Catch an OperationFailedException built-in exception
12try:
13 c.my_exception_function1()
14except RR.OperationFailedException as e:
15 print("Caught OperationFailedException: " + str(e))
16
17# Catch a custom exception
18exp_type = RRN.GetExceptionType("experimental.exception_example.MyExampleCustomException", c)
19try:
20 c.my_exception_function2()
21except exp_type as e:
22 print("Caught MyExampleCustomException: " + str(e))
23
24# Catch RobotRaconteurException to catch all Robot Raconteur exceptions
25try:
26 c.my_exception_function1()
27except RR.RobotRaconteurException as e:
28 print("Caught RobotRaconteurException: " + str(e))
1% exceptions_example.m - Example of using exceptions in MATLAB
2
3% Connect to the service
4url = 'rr+tcp://localhost:53224/?service=exception_example';
5c = RobotRaconteur.ConnectService(url);
6
7% MATLAB does not filter exceptions by type, so any errors are caught by the catch block
8try
9 c.my_exception_function1();
10catch e
11 disp(e.message);
12end
13
14try
15 c.my_exception_function2();
16catch e
17 disp(e.message);
18end
1// exception_example.cs - Example of handling exceptions in C#
2
3using RobotRaconteur;
4using System;
5using System.Diagnostics;
6using System.Linq;
7using System.Collections.Generic;
8
9using (var node_setup = new ClientNodeSetup(args))
10{
11 var c = (experimental.exception_example.ExceptionExample)RobotRaconteurNode.s.ConnectService(
12 "rr+tcp://localhost:53224/?service=exception_example");
13
14 // Catch an OperationFailedException built-in exception
15 try
16 {
17 c.my_exception_function1();
18 }
19 catch (OperationFailedException e)
20 {
21 Console.WriteLine("Caught exception: " + e.Message);
22 }
23
24 // Catch a custom exception
25 try
26 {
27 c.my_exception_function2();
28 }
29 catch (experimental.exception_example.MyExampleCustomException e)
30 {
31 Console.WriteLine("Caught exception: " + e.Message);
32 }
33}
1// container_value_types.cpp - Example of using container value types (maps and lists)
2
3#include <stdio.h>
4#include <iostream>
5#include <boost/range/algorithm.hpp>
6#include <RobotRaconteur.h>
7#include "robotraconteur_generated.h"
8
9// Only use the RR alias in cpp files. Do not use it in header files.
10namespace RR = RobotRaconteur;
11
12int main(int argc, char* argv[])
13{
14 RR::ClientNodeSetup node_setup(ROBOTRACONTEUR_SERVICE_TYPES, argc, argv);
15
16 experimental::exception_example::ExceptionExamplePtr c =
17 RR::rr_cast<experimental::exception_example::ExceptionExample>(
18 RR::RobotRaconteurNode::s()->ConnectService("rr+tcp://localhost:53224/?service=exception_example"));
19
20 // Catch an OperationFailedException built-in exception
21 try
22 {
23 c->my_exception_function1();
24 }
25 catch (const RR::OperationFailedException& e)
26 {
27 std::cout << "Caught OperationFailedException: " << e.what() << std::endl;
28 }
29
30 // Catch a custom exception
31 try
32 {
33 c->my_exception_function2();
34 }
35 catch (const experimental::exception_example::MyExampleCustomException& e)
36 {
37 std::cout << "Caught MyExampleCustomException: " << e.what() << std::endl;
38 }
39
40 std::cout << "exception_example.cpp example complete" << std::endl;
41 return 0;
42}