Error Handling in objective c
Before going to my this tutorial read my previous tutorial here:
Learn Objective C
Error Handling is handle the user level issues by thinking in future while making application. For example: If you make such application having save and fetch file from IOS file directory but user enter wrong information about file or that file does not exist,You have calculator respond to divide button at run time inputs by user but what happen if user enter wrong values like divide some number by 0. In all these cases some times application gets crash or fetch wrong values.
In general for make good and responsive application there are two things to manage errors and exceptions. I will go through all these problem in detail.
Exceptions in Objective C
Exceptions are general error occur during unexpected input like above written second example of divide,stack verflow also example of Exceptions.
In other words Exceptions represents the programming level error.It can be remove by developers after analysing application deeply.What input is required for Application...
In general you have read in programming languages about try,catch,throw,finally mechanism to manage exceptions.
Similarly Exceptions in IOS are manage by try,catch,throw. NSExceptions class is used to manage exceptions in IOS.
Lets us discuss about @try,@catch,@throw,@finally overview.
@try: In try block there is write that code which is expected to get thrown when expection occurs.
@try
{
//code is here.
}
@throw: If any exceptions occur while executing try block then that exceptions detect and thrown by throw keyword.
@try{
//code is here
@throw;
}
@Catch: In catch, catches the exception thrown by throw keyword in try block and try to handle that exceptions.
@catch(NSExceptions *exp)
{
//code is here.
}
The catch statements can be multiple types.
@catch(NSEXcetions *exp1)
{
//first catch block;
}
@catch(NSExceptions *exp2)
{
//second catch block;
}
You can again thrown exceptions from inside catch block if their is need.
@catch(NSExcetions *exp)
{
//catch code is here;
throw;
}
@finally: At the end of program execution by try,catch,throw execute final statements wheteher execeptions occured or not.
@finally
{
//code is here;
}
Here before going to example of objective c i will write simple example on C.
main()
{
int a,b;
printf("Enter the value of a and b");
scanf("%d%d",&a,&b);
try
{
if(a!=0&&b!=0)
{
int x=a/b;
printf("The divison of numbers are:",x);
}
else if(a==0)
{
printf("The divison of numbers are:0")
}
else
{
throw(b);
}
}
catch(int i)
{
printf("Divide by zero: not possible");
}
finally {
printf("Program Executed:");
}
Now in Objective c
NSException *exp=[NSException exceptionWithName:@"no divison possible" reason:@"divide by zero" userInfo:nil];
int a,b;
NSLog(@"Enter value of a and b");
scanf("%d%d",&a,&b);
@try {
if (a!=0 && b!=0) {
int x=a/b;
NSLog(@"The Divison of two numbers are %d",x);
}
else if (a==0)
{
NSLog(@"The divison is zero");
}
else
{
@throw exp;
}
}
@catch (NSException *exception) {
NSLog(@"The Divison is not possible:");
}
@finally {
NSLog(@"Program is executed");
}
Output is shown here:
It is the simple example. You can also use built in function of objective c to throw exceptions
These are NSRangeExceptions
NSInternalInconsistencyException
You can check all these exception by calling exception in NSException class through If statements.
If(exception.name==NSRangeException)
Errors: As i already explain about errros previously the Error occurs at user level unexpected problem. Before going to application into crash state some popup are produced for alerting user like Alert Box.
In general these are the errors displaying in console while running application during development of application in xcode. But when user run this application on device there is no console there to print error so application gets crashed.It is required to inform user about that by the way of alert box printing information.
NSError is generally used to represent information about the error.
It includes generally:
1.)Domain: It desribe the main infomation about the error. what is the problem behind that error.
2.)Code: The error code for that error.
3.)UserInfo: It describes UserInformation for That error.
NSLocalisedDiscriptionKey can be used to fetch key from dictionary.
NSError *error=[[NSError alloc]initWithDomain:@"self.edu" code:101 userInfo:nil];
NSString *values=self.entertext.text;
NSString *print=error.domain;
NSInteger *title=error.code;
NSString *str=[NSString stringWithFormat:@"%d",(int)title];
if(values==nil)
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:str message:print delegate:nil cancelButtonTitle:@"No" otherButtonTitles:nil, nil];
[alert show];
}
else{
self.text.text=values;
}
Source Code Available here:Download here.
I hope you like my this simple tutorial. If you have any query you can mail me or comment here.
My next tutorial is file handling in objective c
Before going to my this tutorial read my previous tutorial here:
Learn Objective C
Error Handling is handle the user level issues by thinking in future while making application. For example: If you make such application having save and fetch file from IOS file directory but user enter wrong information about file or that file does not exist,You have calculator respond to divide button at run time inputs by user but what happen if user enter wrong values like divide some number by 0. In all these cases some times application gets crash or fetch wrong values.
In general for make good and responsive application there are two things to manage errors and exceptions. I will go through all these problem in detail.
Exceptions in Objective C
Exceptions are general error occur during unexpected input like above written second example of divide,stack verflow also example of Exceptions.
In other words Exceptions represents the programming level error.It can be remove by developers after analysing application deeply.What input is required for Application...
In general you have read in programming languages about try,catch,throw,finally mechanism to manage exceptions.
Similarly Exceptions in IOS are manage by try,catch,throw. NSExceptions class is used to manage exceptions in IOS.
Lets us discuss about @try,@catch,@throw,@finally overview.
@try: In try block there is write that code which is expected to get thrown when expection occurs.
@try
{
//code is here.
}
@throw: If any exceptions occur while executing try block then that exceptions detect and thrown by throw keyword.
@try{
//code is here
@throw;
}
@Catch: In catch, catches the exception thrown by throw keyword in try block and try to handle that exceptions.
@catch(NSExceptions *exp)
{
//code is here.
}
The catch statements can be multiple types.
@catch(NSEXcetions *exp1)
{
//first catch block;
}
@catch(NSExceptions *exp2)
{
//second catch block;
}
You can again thrown exceptions from inside catch block if their is need.
@catch(NSExcetions *exp)
{
//catch code is here;
throw;
}
@finally: At the end of program execution by try,catch,throw execute final statements wheteher execeptions occured or not.
@finally
{
//code is here;
}
Here before going to example of objective c i will write simple example on C.
main()
{
int a,b;
printf("Enter the value of a and b");
scanf("%d%d",&a,&b);
try
{
if(a!=0&&b!=0)
{
int x=a/b;
printf("The divison of numbers are:",x);
}
else if(a==0)
{
printf("The divison of numbers are:0")
}
else
{
throw(b);
}
}
catch(int i)
{
printf("Divide by zero: not possible");
}
finally {
printf("Program Executed:");
}
Now in Objective c
NSException *exp=[NSException exceptionWithName:@"no divison possible" reason:@"divide by zero" userInfo:nil];
int a,b;
NSLog(@"Enter value of a and b");
scanf("%d%d",&a,&b);
@try {
if (a!=0 && b!=0) {
int x=a/b;
NSLog(@"The Divison of two numbers are %d",x);
}
else if (a==0)
{
NSLog(@"The divison is zero");
}
else
{
@throw exp;
}
}
@catch (NSException *exception) {
NSLog(@"The Divison is not possible:");
}
@finally {
NSLog(@"Program is executed");
}
Output is shown here:
It is the simple example. You can also use built in function of objective c to throw exceptions
These are NSRangeExceptions
NSInternalInconsistencyException
You can check all these exception by calling exception in NSException class through If statements.
If(exception.name==NSRangeException)
Errors: As i already explain about errros previously the Error occurs at user level unexpected problem. Before going to application into crash state some popup are produced for alerting user like Alert Box.
In general these are the errors displaying in console while running application during development of application in xcode. But when user run this application on device there is no console there to print error so application gets crashed.It is required to inform user about that by the way of alert box printing information.
NSError is generally used to represent information about the error.
It includes generally:
1.)Domain: It desribe the main infomation about the error. what is the problem behind that error.
2.)Code: The error code for that error.
3.)UserInfo: It describes UserInformation for That error.
NSLocalisedDiscriptionKey can be used to fetch key from dictionary.
NSError *error=[[NSError alloc]initWithDomain:@"self.edu" code:101 userInfo:nil];
NSString *values=self.entertext.text;
NSString *print=error.domain;
NSInteger *title=error.code;
NSString *str=[NSString stringWithFormat:@"%d",(int)title];
if(values==nil)
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:str message:print delegate:nil cancelButtonTitle:@"No" otherButtonTitles:nil, nil];
[alert show];
}
else{
self.text.text=values;
}
Source Code Available here:Download here.
I hope you like my this simple tutorial. If you have any query you can mail me or comment here.
My next tutorial is file handling in objective c

I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in IOS development, kindly contact us http://www.maxmunus.com/contact
ReplyDeleteMaxMunus Offer World Class Virtual Instructor led training on IOS development . We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us:
Name : Arunkumar U
Email : arun@maxmunus.com
Skype id: training_maxmunus
Contact No.-+91-9738507310
Company Website –http://www.maxmunus.com