[Java] throw (s) Keyword

throws keyword

A statement of the method,
for a plurality of possible exception thrown by the method specified,
the plurality of intermediate exception classes separated by commas

public class Demo {
	public static void main(String[] args) throws Exception {
		divide(6, 0);
	}
	public static coid divide(int num1, int num2) throws Exception {
		syso(num1 / num2);
	}
}

throw keyword

A method for in vivo,
and an exception is thrown class object

public class Demo {
	public static void main(String[] args) {
		try {
			printAge(23);
		}catch (Exception e) {
			syso(e.getMessage());
		}
	}

	public static void printAge(int age) throws Exception {
		if(age < 0 || age > 200) {
			throw new Expection("年龄出错");
		else {
			syso("年龄是" + age);
		}
	}
}
Published 38 original articles · won praise 4 · Views 806

Guess you like

Origin blog.csdn.net/Hide111/article/details/105309443