Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throwing an exception in objective-c/cocoa

What's the best way to throw an exception in objective-c/cocoa?

like image 762
Steph Thirion Avatar asked Nov 27 '08 17:11

Steph Thirion


People also ask

How do you handle exceptions in Objective-C?

The exception handling mechanisms available to Objective-C programs are effective ways of dealing with exceptional conditions. They decouple the detection and handling of these conditions and automate the propagation of the exception from the point of detection to the point of handling.

Can you throw exceptions in C?

C doesn't support exceptions. You can try compiling your C code as C++ with Visual Studio or G++ and see if it'll compile as-is. Most C applications will compile as C++ without major changes, and you can then use the try... catch syntax.

How do you intentionally throw an exception?

Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.

Is throwing an exception a side effect?

It should be clear that throwing an exception violates referential transparency, and consequently has side-effects.


1 Answers

I use [NSException raise:format:] as follows:

[NSException raise:@"Invalid foo value" format:@"foo of %d is invalid", foo];
like image 157
e.James Avatar answered Oct 05 '22 12:10

e.James