Go to main content
Oracle® Developer Studio 12.6: Debugging a Program with dbx

Exit Print View

Updated: June 2017
 
 

Exception Handling in dbx

A program stops running if an exception occurs. Exceptions signal programming anomalies, such as division-by-zero or array overflow. You can set up blocks to catch exceptions raised by expressions elsewhere in the code.

    While debugging a program, dbx enables you to do the following:

  • Catch unhandled exceptions before stack unwinding

  • Catch unexpected exceptions

  • Catch specific exceptions regardless of whether they are handled before stack unwinding

  • Determine where a specific exception would be caught if it occurred at a particular point in the program

If you issue a step command after stopping at a point where an exception is thrown, control is returned at the start of the first destructor executed during stack unwinding. If you step out of a destructor executed during stack unwinding, control is returned at the start of the next destructor. When all destructors have been executed, a step command brings you to the catch block handling the throwing of the exception.

Commands for Handling Exceptions

This sections describes the dbx commands for handling exceptions.

exception Command

The syntax for the exception command is as follows:

exception [-d | +d]

Use the exception command to display an exception’s type at any time during debugging. If you use the exception command without an option, the type shown is determined by the setting of the dbxenv variable output_dynamic_type:

  • If it is set to on, the derived type is shown.

  • If it is set to off (the default), the static type is shown.

    Specifying the -d or +d option overrides the setting of the environment variable.

  • If you specify -d, the derived type is shown.

  • If you specify +d, the static type is shown.

For more information, see exception Command.

intercept Command

The syntax for the intercept command is as follows:

intercept [-all] [-x] [-set] [typename]

You can intercept, or catch, exceptions of a specific type before the stack has been unwound.

  • Use the intercept command with no arguments to list the types that are being intercepted.

  • Use -all to intercept all exceptions. Use typename to add a type to the intercept list.

  • Use -x to exclude a particular type to the excluded list to keep it from being intercepted.

  • Use -set to clear both the intercept list and the excluded list, and set the lists to intercept or exclude only throws of the specified types.

For example, to intercept all types except int:

(dbx) intercept -all -x int

To intercept exceptions of type Error:

(dbx) intercept Error

After intercepting too many CommonError exceptions with the following command:

(dbx) intercept -x CommonError

Typing the intercept command with no arguments would then show that the intercept list includes unhandled exceptions and unexpected exceptions, which are intercepted by default, plus exceptions of class Error except for those of class CommonError.

(dbx) intercept
-unhandled   -unexpected   class Error -x class CommonError

If you then realize that Error is not the class of exceptions that interests you, but you do not know the name of the exception class you are looking for, you could try intercepting all exceptions except those of class Error by typing:

(dbx) intercept -all -x Error

For more information, see intercept Command.

unintercept Command

The syntax for the unintercept command is as follows:

unintercept [-all] [-x] [typename]
  • Use the unintercept command to remove exception types from the intercept list or the excluded list.

  • Use the command with no arguments to list the types that are being intercepted (same as the intercept command).

  • Use -all to remove all types from the intercept list. Use typename to remove a type from the intercept list. Use -x to remove a type from the excluded list.

For more information, see unintercept Command.

whocatches Command

The whocatches command reports where an exception of typename would be caught if thrown at the current point of execution. Use this command to find out what would happen if an exception were thrown from the top frame of the stack.

The line number, function name, and frame number of the catch clause that would catch typename are displayed. The command returns type is unhandled” if the catch point is in the same function that is doing the throw.

For more information, see whocatches Command.

Examples of Exception Handling

This example demonstrates exception handling in dbx by using a sample program containing exceptions. An exception of type int is thrown in the function bar and is caught in the following catch block.

1  #include <stdio.h>
2
3  class c {
4      int x;
5    public:
6      c(int i) { x = i; }
7      ~c() {
8                printf("destructor for c(%d)\n", x);
9           }
10  };
11
12  void bar() {
13      c c1(3);
14      throw(99);
15  }
16
17  int main() {
18      try {
19          c c2(5);
20          bar();
21          return 0;
22      }
23      catch (int i) {
24          printf("caught exception %d\n", i);
25      }
26  }

The following transcript from the example program shows the exception handling features in dbx.

(dbx) intercept
-unhandled -unexpected
(dbx) intercept int
<dbx> intercept
-unhandled -unexpected int
(dbx) stop in bar
(2) stop in bar()
(dbx)run
Running: a.out
(process id 304)
Stopped in bar at line 13 in file “foo.cc”
   13       c c1(3);
(dbx) whocatches int
int is caught at line 24, in function main (frame number 2)
(dbx) whocatches c
dbx: no runtime type info for class c (never thrown or caught)
(dbx) cont
Exception of type int is caught at line 24, in function main (frame number 4)
stopped in _exdbg_notify_of_throw at 0xef731494
0xef731494: _exdbg_notify_of_throw          :        jmp     %o7 + 0x8
Current function is bar
   14        throw(99);
(dbx) step
stopped in c::~c at line 8 in file "foo.cc"
    8         printf("destructor for c(%d)\n", x);
(dbx) step
destructor for c(3)
stopped in c::~c at line 9 in file "foo.cc"
    9       }
(dbx) step
stopped in c::~c at line 8 in file "foo.cc"
    8        printf("destructor for c(%d)\n", x);
(dbx) step
destructor for c(5)
stopped in c::~c at line 9 in file "foo.cc"
    9       )
(dbx) step
stopped in main at line 24 in file "foo.cc"
   24           printf("caught exception %d\n", i);
(dbx) step
caught exception 99
stopped in main at line 26 in file "foo.cc"
   26   }

Note -  The examples used in this section were built with the Oracle Developer Studio compilers. The examples would differ if compiling the code with gcc.