How can I raise an error in if-else function in java

Here you go:

throw new java.lang.Error("this is very bad");

More idiomatic to throw a subclass of Exception. RuntimeException in particular is unchecked (e.g., methods don’t need to declare that they might throw it). (Well, so is Error, but it’s supposed to be reserved for unrecoverable things).

throw new java.lang.RuntimeException("this is not quite as bad");

Note: you don’t actually have to construct them right at that moment. You can throw pre-constructed ones. But one thing nice about constructing them is they record the line of code they were on and the complete call-stack that happened at the time of construction, and so constructing a new one right when you throw it does inject it with very helpful diagnostic information.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)