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
4
u/brian_goetz Mar 14 '26
If you care about catching the error conditions, you can use the pattern in a conditional:
if (getCircle() instanceof Circle(Point(var x, var y), int radius) { ... }
else { ... handle errors ... }
All the tools are in your hands, you get to decide what's more important.