Showing posts with label throw. Show all posts
Showing posts with label throw. Show all posts

Monday, 27 June 2011

More precise exception rethrow

The more precise rethrow is a tricky one to understand. See if you can spot what the new feature is in the example below. Will it compile on pre-java-7? If not how would it need to be changed to compile on pre-java-7? The answers lie after the example.


public class MorePreciseExceptionRethrow {

static class Exception1 extends Exception {}
static class Exception2 extends Exception {}

public static void main(String[] args) throws Exception1, Exception2 {
try {
boolean test = true;
if (test) {
throw new Exception1();
} else {
throw new Exception2();
}
} catch (Exception e) {
throw e;
}
}

}


On Java 6 compiling the above gives the following exception.
Foo.java:18: unreported exception java.lang.Exception; must be caught or declared to be thrown
throw e;
^
1 error

This can be fixed for Java 6 by changing:
public static void main(String[] args) throws Exception1, Exception2 {

to:

public static void main(String[] args) throws Exception {{

So now you see the improvement that Java 7 offers with this feature. You can be more precise in the declaration of the exceptions that you rethrow. Very nice indeed.

Monday, 16 May 2011

throw vs throws in java

throws is used to specify that an exception/s can be thrown. Specifying is more pertinent to checked exceptions.

throw is simply the statement to manually/programically throw the exception or any object of throwable type.

Using throws

a method, and then tell that this method throws following exception…and then write da .. da .. da. Eg.

public void myMethod() throws MalformedURLException,MyCustomException1
{
//do something in method
}



Using throw


throw just throws exception. Eg.

if(something)
throw someObjectOfThrowableType

Saturday, 12 March 2011

How to Throw Exceptions

The pages listed below explain how to throw exceptions in a Java program.

The throw Statement

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

The Throwable Class and Its Subclasses

The Java language requires that exceptions derive from the Throwable class or one of its subclasses.

Creating Your Own Exception Classes

As you have undoubtedly noticed in your travels (travails?) through the Java language, the packages that ship with the Java development environment provide numerous exception classes. All of these classes are descendants of the Throwableclass and allow programs to differentiate between the various types of exceptions that can occur during the execution of a Java program.
You can create your own exception classes, as well, to represent problems that can occur within the classes that you write. Indeed, if you are a package developer you will find that you must create your own set of exception classes to allow your users to differentiate an error that can occur in your package versus those errors that occur in the Java development environment or other packages.

The throw Statement

Syntax of throw

All Java methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. In the Java system, throwable objects are instances of any subclass of the Throwable class. Here's an example of a throw statement:
throw someThrowableObject;


What if object is not Throwable ?

If you attempt to throw an object that is not throwable, the compiler refuses to compile your program and displays an error message similar to the following:

testing.java:10: Cannot throw class java.lang.Integer; it must be a subclass of class java.lang.Throwable.
throw new Integer(4);
^

The next page, The Throwable Class and Its Subclasses, talks more about the Throwable class.


Example



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


public Object pop() throws EmptyStackException {
Object obj;
if (size == 0)
throw new EmptyStackException();
obj = objectAt(size - 1);
setObjectAt(size - 1, null);
size--;
return obj;
}

The pop method checks to see if there are any elements on the stack. If the stack is empty (its size is equal to 0), then pop instantiates a new EmptyStackException object and throws it. The EmptyStackException class is defined in the java.util package. Later pages in this lesson describe how you can create your own exception classes. For now, all you really need to remember is that you can throw only objects that inherit from the java.lang.Throwable class.

The throws Clause





You'll notice that the declaration of the pop method contains this clause:



throws EmptyStackException

The throws clause specifies that the method can throw an EmptyStackException. As you know, the Java language requires that methods either catch or specify all checked exceptions that can be thrown within the scope of that method. You do this with the throws clause of the method declaration. For more information about this requirement see Java's catching or throwing exception. Also, Specifying the Exceptions Thrown by a Method shows you in more detail how a method can specify the exceptions it can throw.





throw vs throws


See here for the difference between throw and throws.