(Java) Throws and Throw

Before you can catch an exception, some code somewhere must throw one. Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the Java platform, or the Java runtime environment. Regardless of what throws the exception, it’s always thrown with the throw statement.

As you have probably noticed, the Java platform provides numerous exception classes. All the classes are descendants of the Throwable class, and all allow programs to differentiate among the various types of exceptions that can occur during the execution of a program.

You can also create your own exception classes to represent problems that can occur within the classes you write. In fact, if you are a package developer, you might have to create your own set of exception classes to allow users to differentiate an error that can occur in your package from errors that occur in the Java platform or other packages.

You can also create chained exceptions. For more information, see the Chained Exceptions section.

The throw Statement

All methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class. Here’s an example of a throw statement.

throw someThrowableObject;
Let’s look at the throw statement in context. The following pop method is taken from a class that implements a common stack object. The method removes the top element from the stack and returns the object.

public Object pop() {
    Object obj;

    if (size == 0) {
        throw new EmptyStackException();

    obj = objectAt(size - 1);
    setObjectAt(size - 1, null);
    return obj;

import java.io.IOException;
import java.nio.channels.FileLockInterruptionException;
public class Throwssss {
	public static void main(String[] args)throws FileLockInterruptionException,
		//FileInputStream inputStream = new FileInputStream("buka file.txt");
			System.out.println("Kode setelah buka file");



Example Throws in exception in put in a method and in calling on the exception.

import java.io.FileNotFoundException;
import java.io.IOException;

public class ThrowsException {
	public static void main(String[] args) {
		try {
		} catch(FileNotFoundException ex){
		} catch(IOException ex) {
		System.out.println("kode di dalam method main");
		public static void methodTidakBertanggungJawab() throws FileNotFoundException,
		IOException {
		System.out.println("kode setelah buka file");


Reference :
Ebook Java Desktop Ifnu Bima


Published by


hello I am jarcode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s