Why does `error.Foo catch {};` compiles, error.Foo is error, not error union - zig

This code compiles:
error.Foo catch {};
But not:
error.Foo catch |bar| {
std.debug.print("{s}", .{bar});
};
Why is that? Does catch without capturing payload works with error too? I thought catch only works with error union.

Related

Why do I receive Unhandled exception in Dart? I think to catch everything

Why I read "Unhandled exception" in console for the "_udpSocket!.send" line?
Am I not catching all errors and exceptions?
try {
_udpSocket!.send(_packet,_serverAddress!,Server_UdpPort); //Send packet
} catch (exception,stack) {
int _doNothing;
}

Decode a response from a catch in Dart

I try to decode the response returned from a catch, here is what I tried :
try{
...
} catch (err) {
//JsonCodec codec = new JsonCodec(); // doesn't work
//var decoded = codec.decode(err);
...
}
Error: type '_Exception' is not a subtype of type 'String'
print(err) :
Exception: {
"error": {
"code": "invalid_expiry_year"
}
}
I would like to get the value of "code", I tried many things but it doesn't work,
Any idea?
print(err.code);
then I get :
You could get the message property and jsonDecode it.
try {
} catch (e) {
var message = e.message; // not guaranteed to work if the caught exception isn't an _Exception instance
var decoded = jsonDecode(message);
print(decoded['error']['code'])'
}
All of this strikes as a misuse of Exception. Note that you generally don't want to throw Exception(message);. Putting a json encoded message in there is not the best way to communicate details about the exception. Instead, write a custom implementation of Exception and catch the specific type.
class InvalidDataException implements Exception {
final String code;
InvalidDataException(this.code);
}
try {
} on InvalidDataException catch(e) {
print(e.code); // guaranteed to work, we know the type of the exception
}
See the docs for Exception:
Creating instances of Exception directly with new Exception("message") is discouraged, and only included as a temporary measure during development, until the actual exceptions used by a library are done.
See also https://www.dartlang.org/guides/language/effective-dart/usage#avoid-catches-without-on-clauses
I finally figured out a solution :
catch (response) {
var err=response.toString().replaceAll('Exception: ', '');
final json=JSON.jsonDecode(err);
print(json['error']['code']);
}

'no calls to throwing functions occur within 'try' expression [warning] in swift 3

I'm trying to delete an object from core data and I tried it within try catch as below.
do {
try self.managedContext.deleteObject(self.productList[indexPath.row])
} catch let error as NSError {
print("something wrong with the delete : \(error.userInfo)")
}
it say 'no calls to throwing functions occur within 'try' expression and 'catch block is unreachable because no errors are throw in 'do' block. following image give you more idea.
why is this. I have no idea. how to solve this. hope your help.
The deleteObject method doesn't throw. Remove the Do-Catch block and the warning will go away.
Make sure that the method that you are invoking after try throws.
Example:
func someThrowingFunction() throws -> Int {
// ...
}
let x = try? someThrowingFunction()
let y: Int?
do {
y = try someThrowingFunction()
} catch {
y = nil
}
more information about that here: ErrorHandling

Nil is not compatible with expected argument type '()'

I am new in swift and found that RSSReader code from internet and getting error in swift2.
class func saveManagedObjectContext(managedObjectContext:NSManagedObjectContext)->Bool{
if managedObjectContext.save(nil){
return true
}else{
return false
}
}
Nil is not compatible with expected argument type '()'
Call can throw, but it is not marked with 'try' and the error is not handled
Can anyone tell me how i can fix it in swift2?
Thanks
Remove nil from the argument list. The method managedObjectContext.save() throws an error in case something goes wrong. The right way of doing it is
do{
try managedObjectContext.save()
return true
}
catch{
return false
}
The save() method does not take any parameters, so using nil as a parameter is both redundant and invalid. Also, when calling the save method, it has the possibility of throwing an error, so you have to program your function to handle that possible error, like so:
func saveManagedObjectContext(managedObjectContext:NSManagedObjectContext)->Bool {
do {
try managedObjectContext.save()
return true
} catch {
return false
}
}
If you have specific errors you want to catch, the syntax is written like so:
catch [errorNameHere] {
[codeToRun]
}
And if you want to catch multiple errors and run corresponding code, you can write this:
catch [errorNameHere] {
[codeToRun]
} catch [anotherErrorNameHere] {
[codeToRun]
} catch {
[defaultCodeToRun] /* if no errors are thrown that were written above, but
there is an error thrown, this default catch block will handle it. If there
is no catch block to handle an error thrown and no default catch block, the
compiler will simply exit without having run anything. */
}
You can read all about error handling in the Swift Documentation here.

Suddenly get do/catch error

I've been running this code for quite sometime however suddenly it throws error below, how come i suddenly get this without editing the code? and what am i doing wrong in my do/catch statement?
func addInput(device: AVCaptureDevice) {
do {
captureSession.addInput(try AVCaptureDeviceInput(device: device))
} catch let err as NSError {
print(err)
}
}
error
Errors thrown from here are not handled because the enclosing catch is not exhaustive
There is the possibility of a catch with or without error. Just add a new catch statement.
do { /* try something */ }
catch let error { print((error as NSError)) }
catch { print("No error") }
If you use the version in the second line you might not need the third line.

Resources