Errors
Every method in a Hybrid Object can throw an error using the language-default error throwing feature:
- Swift
- Kotlin
- C++
HybridMath.swift
class HybridMath : HybridMathSpec {
public func add(a: Double, b: Double) throws -> Double {
if a < 0 || b < 0 {
throw RuntimeError.error("Value cannot be negative!")
}
return a + b
}
}
HybridMath.kt
class HybridMath : HybridMathSpec() {
override fun add(a: Double, b: Double): Double {
if (a < 0 || b < 0) {
throw Error("Value cannot be negative!")
}
return a + b
}
}
HybridMath.hpp
class HybridMath: public HybridMathSpec {
double add(double a, double b) override {
if (a < 0 || b < 0) {
throw std::runtime_error("Value cannot be negative!");
}
return a + b;
}
};
Errors will be propagated upwards to JS and can be caught just like any other kind of error using try
/catch
:
`Math.add(...)`: Value cannot be negative!