Throws Keyword Example in Java

import java.io.IOException;

class ExceptionThrowsExample {

	void methodOne() throws ClassNotFoundException {
		throw new ClassNotFoundException(
				" methodOne() - ClassNotFound Exception");
	}

	void methodTwo() throws IOException {
		throw new IOException("  methodTwo() - IO Exception");
	}

	public static void main(String args[]) {

		ExceptionThrowsExample obj = new ExceptionThrowsExample();
		System.out
				.println("Before calling ExceptionThrowsExample Class methods...");
		try {
			obj.methodOne();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		try {
			obj.methodTwo();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out
				.println("After calling ExceptionThrowsExample Class methods...");
	}
}
Output
Before calling ExceptionThrowsExample Class methods... java.lang.ClassNotFoundException: methodOne() - ClassNotFound Exception at test.ExceptionThrowsExample.methodOne(ExceptionThrowsExample.java:8) at test.ExceptionThrowsExample.main(ExceptionThrowsExample.java:22) java.io.IOException: methodTwo() - IO Exception at test.ExceptionThrowsExample.methodTwo(ExceptionThrowsExample.java:13) at test.ExceptionThrowsExample.main(ExceptionThrowsExample.java:28) After calling ExceptionThrowsExample Class methods...










Related Examples


Write a program to check if an input String is Palindrome or not?
Calculate the factorial of a given number using Java?
How to reverse a number in Java?
Write a program to convert from decimal to binary?
Producer Consumer Example in Thread
Write a program to reverse a String?








Receive Latest Java Examples in Your Email: