Java, brings with it several Exceptions already ready, IllegalArgumentException, IllegalStateException, RuntimeException, among others.
How to create a Custom Exception in Java?
I have the following method - sumAndPrint(int,int)
Add two integers and prime them to the Output associated with System.
public void sumAndPrint(int x, int y) throws Exception{
if(x < 0 || y < 0){
//trocar uma excessao específica - NumeroNegativoException
throw new Exception("Numeros negativos nao permitidos");
}
System.out.println("soma -> " + (x+y));
}
However, I do not accept arguments less than zero.
I want to create my own exception (checked exception) - NumeroNegativoException
- how do I do this?
The purpose of this question / answer is to share knowledge about how to create exceptions in Java.