Reputation: 1729
Is it possible to prevent application crash ?
In many applications i experience many crash but sometimes Its really tough to find the reason of crash and it becomes time consuming to find the solution.
I just wanna know is there any code snippet which can give you what are the crash reasons or can we prevent it using @try-@catch.
If it can be avoid by @try-@catch, then how, where should i place the try-catch?
Upvotes: 1
Views: 3886
Reputation: 1729
I was looking for this solution. The more descriptive trace can be printed with the following method. I think this can help to find the proper reason.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
}
void uncaughtExceptionHandler(NSException *exception) {
NSLog(@"CRASH: %@", exception);
NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
}
The application crash cannot be prevented. But we can get the more detailed trace log using it. I accept that my question is wrongly asked and uncleared. Sorry for being unclear.
I was again spending time on this. Finally i got the article and demo which will catch your exceptions or BAD_ACCESS in your apps. So this would be very helpful for all developers to give feature like SUBMIT BUG in their apps so whenever users get any exceptions they can continue or quit or submit a crash report to developers. Its really really helpful. I am gonna implement in my all projects.
Here is the article by Matt Gallagher
http://cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html
Thanks to Matt Gallagher.
Upvotes: 9
Reputation: 5876
You can use try-catch-finally block to avoid app crash as follows:
-(void)functionInsideWhichAppIsCrashing
{
@try
{
//Your crashing code goes here
}
@catch ( NSException *e )
{
NSLog(@"Crash Reason:@"%@", [e reason]);
}
@finally {// Do whatever you want to do in crash situation
}
}
Upvotes: 2
Reputation: 21893
It is possible to prevent a crash by analyzing the crash, diagnosing the cause, and writing or modifying your code to prevent it.
Unfortunately, software development is more than just the initial typing. Testing and bug-fixing is where you spend the vast majority of your time. If that's too tedious for you, I recommend a different occupation, because that's just how this is.
Upvotes: 12