How to Throw Exceptions in Java

class MyException extends Exception {
public MyException(String msg){
super(msg);
}
}
public class Test {

static intĀ  divide(int first,int second) throws MyException{
Ā  if(second==0)
Ā  throw new MyException(“can’t be divided by zero”);
return first/second;
Ā }

Ā public static void main(String[] args) {
Ā  try {
System.out.println(divide(4,0));
Ā  }
catch (MyException exc) {
exc.printStackTrace();
Ā  }
Ā  }
}

click here for more

Leave a comment

Hey!

I’m Bedrock. Discover the ultimate Minetest resource – your go-to guide for expert tutorials, stunning mods, and exclusive stories. Elevate your game with insider knowledge and tips from seasoned Minetest enthusiasts.

Join the club

Stay updated with our latest tips and other news by joining our newsletter.

Design a site like this with WordPress.com
Get started