How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Objective-C programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Objective-C programming language

Table of Contents

Problem Statement

Show how to create a user-defined exception   and   show how to catch an exception raised from several nested calls away.

Show/describe what happens when the program is run.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Objective-C programming language

Source code in the objective-c programming language

@interface U0 : NSObject { }
@end
@interface U1 : NSObject { }
@end
@implementation U0
@end
@implementation U1
@end

void foo();
void bar(int i);
void baz(int i);

void foo() {
  for (int i = 0; i <= 1; i++) {
    @try {
      bar(i);
    } @catch (U0 *e) {
      NSLog(@"Function foo caught exception U0");
    }
  }
}

void bar(int i) {
  baz(i); // Nest those calls
}

void baz(int i) {
  if (i == 0)
    @throw [U0 new];
  else
    @throw [U1 new];
}


int main (int argc, const char * argv[]) {
  @autoreleasepool {

    foo();

  }
  return 0;
}


  

You may also check:How to resolve the algorithm Hamming numbers step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Munching squares step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Execute HQ9+ step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the ALGOL W programming language