|
1 <?xml version="1.0" encoding="utf-8"?> |
|
2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. --> |
|
3 <!-- This component and the accompanying materials are made available under the terms of the License |
|
4 "Eclipse Public License v1.0" which accompanies this distribution, |
|
5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". --> |
|
6 <!-- Initial Contributors: |
|
7 Nokia Corporation - initial contribution. |
|
8 Contributors: |
|
9 --> |
|
10 <!DOCTYPE concept |
|
11 PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd"> |
|
12 <concept xml:lang="en" id="GUID-E65D91AE-482F-5592-B83C-0F29126C2EFA"><title>Using Signals to Handle Exceptions </title><prolog><metadata><keywords/></metadata></prolog><conbody><p>Signals provide an alternative method (to checking return values) of handling exceptions or failures. The following example code demonstrates how you can check the return value for a failure and also how you can register a handler for handling the failure asynchronously: </p> <codeblock id="GUID-49B4A39A-34F5-54D1-AD97-5AD50EFD9786" xml:space="preserve">#include <signal.h> |
|
13 #include <stdio.h> |
|
14 int func() |
|
15 { |
|
16 int success = 1; |
|
17 // function logic |
|
18 // success == 1 - Indicates that the function logic succeeded |
|
19 // success == 0 - Indicates that the function logic failed |
|
20 if(success) |
|
21 return 0; |
|
22 else |
|
23 { |
|
24 raise(SIGUSR1); |
|
25 return -1; |
|
26 } |
|
27 } |
|
28 void sighandler(int signum) |
|
29 { |
|
30 if(signum == SIGUSR1) |
|
31 // 'signal' method for checking failure |
|
32 printf(“Error: An error occured in func().”); |
|
33 else |
|
34 printf(“Error: Unknown signal”); |
|
35 } |
|
36 int main(void) |
|
37 { |
|
38 int retval = 0; |
|
39 // When SIGUSR1 arrives, invoke sighandler() |
|
40 signal(SIGUSR1,sighandler); |
|
41 retval = func(); |
|
42 if(retval == -1) |
|
43 // The return value method used for checking failure |
|
44 printf("Error: An error occured in func()."); |
|
45 }</codeblock> </conbody><related-links><link href="GUID-66C1493D-5B85-558A-9A39-454E6EBA307B.dita"><linktext>Signal Emulation on Symbian |
|
46 Platform</linktext> </link> <link href="GUID-186B9876-2A08-5F23-BB49-49EC34C51507.dita"><linktext>Using Signals to Terminate Processes</linktext> </link> <link href="GUID-6CF8A41B-C2DD-5D57-A71D-6405CE08A06B.dita"><linktext>Using Signals to Handle Asynchronous |
|
47 Events</linktext> </link> </related-links></concept> |