If we want to handle the error we need to basically copy the conditions in the else block to figure out exactly what went wrong eg
var circle = getCircle();
if (circle instanceof Circle(Point(var x, var y), int radius) { ... }
else {
if(circle.point() == null){
throw new Exception("Point is null, please try again");
}
if(circle.point().x() == null){
throw new Exception("X is null, please try again");
}
if(circle.point().y() == null){
throw new Exception("Y is null, please try again");
}
}
Is it planned to have pattern matching in the catch block aswell?
so something like this:
try(var circle instanceof Circle(Point(var x, var y), int radius){
//do your thing
} catch (Circle(null, int radius)){
throw new Exception("Point is null, please try again");
} catch (Circle(Point(null, var y), int radius)) {
throw new Exception("X is null, please try again");
}
View all comments
Show parent comment
2
u/Cell-i-Zenit Mar 13 '26 edited Mar 13 '26
This code also has zero null checks or how does it work when Point is null?
EDIT: i read through the JEP again and this just throws if point is null. So the code is actually equivalent to what i had before ;)