Using Exception to Jump Out of Functions in Objective-C

As I was developing iOS applications, one problem came out when I tried to handle runtime error in the application.

The problem can be described as follows:
Here we have three functions:
- (void)A;
- (void)B;
- (void)C;

in their implementations, A calls B, while B calls C.
During the runtime, application may receive wrong inputs from users, for example an invalid string or number.
Of course, stopping the application will be a solution, but it is quite bad user experience.

Here I learnt to use Exception mechanism to handle such runtime error.

In function A, or somewhere else you want your program to jump when error is detected, write like this:

@try{
[self B]
@catch (NSException *exception){
//do something using exception
}

While in function where error is detected:

if(ERROR)
@throw [NSException exceptionWithName:@"error title" reason:@"This is error reason" userInfo:nil];

In this way, if user happens to enter an invalid input which causes error in C, the program will then be directed back to A without any complex return.

P.S. It seems that setjmp and longjmp are no longer supported in Objective-C since its 2.0 version.

iOS Development — Just an amateur

I’m currently interested in developing iOS applications with Xcode 4. Frankly speaking, I do not have that much experience in programming with objective-c. I am just an amateur.

I started my development through a Single View Application, which is a template in Xcode environment. In this application I implemented a simple user interface in Interface Builder (which has recently been integrated in Xcode 4, instead of being a stand-along application).

Objective-C is the language used in iOS development as well as in Mac as recommended. As you can guess from its name, it is indeed an object-oriented programming language. However, it is a strange language. You could hardly find any parentheses () in the codes written in it. It sometimes makes beginner wonder where are the functions (or methods). Unlike other method declarations in c or c++, methods in Objective-C are organized in form like [object method:parameter]. A knowledge of this will enable you to read code written by others.

Reading sample codes as well as those written by others is the fastest way to get familiar with a programming language. But this will not be acted as an excuse of plagiarism.

After reading some tutorials, I could write myself a Hello World application based on my attempts. It is just a start!

$latex E=mc^2 &s=4$

E=mc^2

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!