Why Java creators decided not to throw any error or exception when overflow or underflow hapens considering the fact that such scenarios are usually lethal for any software.
Related
There is a note in the Swift Docs that states the following:
Error handling in Swift resembles exception handling in other languages, with the use of the try, catch and throw keywords. Unlike exception handling in many languages—including Objective-C—error handling in Swift does not involve unwinding the call stack, a process that can be computationally expensive. As such, the performance characteristics of a throw statement are comparable to those of a return statement.
What does unwinding the call stack mean in Swift and Obj-c? Related question here but it is C++ specific. I know what the call stack is, but would like a more detailed explanation of unwinding.
If Swift doesn't unwind the call stack, what does it do instead?
Why can this be computationally expensive?
Summed up: I'd like to get a better understanding of how exceptions work and the execution flow in Swift.
Unwinding the stack basically means that when an exception is thrown, the method is immediately interrupted, the caller of the method is immediately interrupted and so on until an exception handler (try-catch-finally) is found or until we reach the top of the stack, when the exception usually ends in interrupting the current thread.
That works pretty well in languages with a garbage collector but in general it can lead to memory leaks in languages with manual memory management. Also, since methods are interrupted in unexpected places, an exception often leads to undefined/unrecoverable program states.
That's why exception in all languages should be used sparingly and only to handle exceptional situations, not to handle normal program flow.
Obj-C exceptions weren't very good, with all the problems mentioned above (see NSException, #try-#catch-#finally), that's why nobody is using them. Instead, Obj-C came with error parameters (you pass a reference to a NSError variable and if the method fails, the error gets assigned into that variable). See Error Handling in Objective-C
Swift just came with another syntax for NSError. It's not a real exception handling (errors don't interrupt program execution). See Error Handling in Swift
Technically, every function/method that can throw an error in Swift only has an additional hidden parameter that is used to pass the error back to caller context.
If you want more information, just compare code in Swift 1.x and 2.x (1.x didn't have special grammar for error handling yet).
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I know this might be easy for some older andf more experienced VB.Net developers but me as a developer starting out a learning I wanted to know what "Try" and "Catch" is used for.
try, catch, finally, and throw are core to an Exception Handling framework, which is a way to communicate an "exceptional" condition in the code -- most notably errors.
If you are using libraries or frameworks, chances are they have a chance to throw an exception if something fails, and it is your code's responsibility to catch and handle that exception (logging, display to user, try to fix the condition and try again, etc.)
Example (in C#-ish pseudocode, but hopefully you'll get the gist:
try {
doSomething();
}
catch (Exception ex) {
log("doSomething() failed: " + ex.getMessage() + " - " + ex.getStackTrace());
}
You'll see that the exception contains at least three types of useful information:
The type of exception it is.
The message contained in the exception
(set by the code that threw the exception), and
The stack trace,
which traces the call stack to the spot the module, line number, and
error occurred.
Hope that starts you on your path to learning how to handle exceptions in your code.
It is used to catch any exceptions that are thrown. If any statement inside the try block throws an exception, the catch block is executed. If there is a catch block associated to the specific exception thrown, that block is executed. Here is the doc that goes into more detail.
When any exception detected by .net in your try scope, it goes to catch scope, and then you can handle the problem in the catch
I am porting a C# application to mac osx,
In the C# version of the program we have a try catch() block which catches any exceptions thrown by the program , which may be nullpointer exceptions or anything we've not caught..
The top level exception handler then asks the user if he likes to report it, when the user says report,
the exception stack is reported to us. which helps identify bugs.
In my OSX app the backend part of the program in C/C++ with User interface in Objective C
Is there a similar feature in an objective C program ?? if not is there any alternate way to report, exception or bug report from within the program ?
As far as exceptions go, NSError is actually preferred over exceptions in Objective-C for non-fatal errors. This is described in Apple's Exception Programming Topics: Introduction to Exception Programming Topics for Cocoa documentation:
Important: You should reserve the use of exceptions for programming or unexpected runtime errors such as out-of-bounds collection access, attempts to mutate immutable objects, sending an invalid message, and losing the connection to the window server.
...
Instead of exceptions, error objects (NSError) and the Cocoa error-delivery mechanism are the recommended way to communicate expected errors in Cocoa applications. For further information, see Error Handling Programming Guide.
Using NSError to Great Effect is a good tutorial on NSError (in addition to the afore-linked error handling documentation).
As far as crash/fatal error reporting goes, see the "Crash Reporter for Cocoa app" question for a number of the options.
I recently started learning Objective-C, and I am working on an iOS app as an exercise, anyway, I want to handle overflow by throwing exception (I come from a Java background), I searched the reference there is only NSException, but then I read in the section that say topics about exception handling, and they said to use NSError, I read the reference but they had the same protocol and methods, so what's the difference between them? And which is better?
Also, I want to create my own exception or error class, are there any methods or fields that I should include? (Like when implementing the Exception interface in Java).
Thanks
NSError is designed for non-fatal, recoverable errors. The problems that are designed to be captured by an NSError are often user errors (or are errors that can be presented to the user), can often be recovered from (hence -presentError: and NSErrorRecoveryAttempting), and are usually expected or predictable errors (like trying to open a file that you don't have access to, or trying to convert between incompatible string encodings).
NSException is designed for potentially fatal, programmer errors. These errors are designed to signify potential flaws in your application where you have not correctly checked the pre-conditions for performing some operations (like trying to access an array index that is beyond its bounds, or attempts to mutate an immutable object). The introduction to the Exception Programming Guide explains this a little bit.
Breaking on Objective-C exceptions is really useful and easily the best way to debug issues with NSArray and the like. However, exceptions are also a great thing to use while actually programming.
Xcode offers two choices for breaking on Obj-C exceptions:
Break whenever an exception is thrown
Break whenever an exception is caught
Breaking on catch seems to be basically useless, since the point of #throw is a lot more important. However, if I'm handling an exception fine, I don't want my program to stop.
So, the ideal situation would be: break on all exceptions that are not caught by my code, but show a stack trace for when the exception was thrown.
Another decent solution would be some sort of debugging whitelist for exceptions that should not be broken on.
Is there any way to filter exception breakpoints?