-3

I know that each java class should extend the Throwable class so that exceptions can be handled. Is this done by:

public class Test extends Throwable

or

public class Test throws Throwable?
everton
  • 7,579
  • 2
  • 29
  • 42
user3126119
  • 323
  • 1
  • 3
  • 10
  • 10
    Where did you read this? – Rohit Jain Jan 20 '14 at 19:32
  • 5
    _What_? This is completely nonsensical. – Louis Wasserman Jan 20 '14 at 19:33
  • 1
    Your information is very, very wrong. If you want to learn how to handle (and throw) exceptions, I'd check out the tutorial on Oracle's site: http://docs.oracle.com/javase/tutorial/essential/exceptions/ (and in particular the section about [handling exceptions](http://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html)) – yshavit Jan 20 '14 at 19:34
  • i already read it but it did not help me with this. my lecturer's notes have that in Java to catch exceptions you need to extend the throwable class – user3126119 Jan 20 '14 at 19:36
  • quoted: " Primitive types, or object whose class doesn’t extend Throwable cannot be thrown as exceptions." – user3126119 Jan 20 '14 at 19:37
  • @user3126119 That statement is correct, but you have misread it. – Marko Topolnik Jan 20 '14 at 19:37
  • @MarkoTopolnik can you explain please – user3126119 Jan 20 '14 at 19:38
  • @user3126119 Your comment, and the statement in your question have 2 different meanings. Anyways, yes that is true. – Rohit Jain Jan 20 '14 at 19:38
  • 2
    **Throwing** an exception is very different from **handling** it. That's like saying that a catcher in baseball needs a ball in order to catch a ball. He doesn't need a ball, he needs a glove. To handle an exception, you don't extend `Throwable`, you write a `try-catch` block. – yshavit Jan 20 '14 at 19:39

3 Answers3

3

Primitive types, or object whose class doesn’t extend Throwable cannot be thrown as exceptions.

Interpretation:

throw 3;

doesn't work because 3 is a primitive type.

throw new String();

doesn't work because String doesn't extend Throwable.

throw new RuntimeException();

works because RuntimeException is a subclass of Throwable.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Actually you can `throw null` which will do what you expect :) – Adam Gent Jan 20 '14 at 19:41
  • 2
    @AdamGent `null`'s type, also called `null` by the specification, is by definition the subtype of all reference types, which includes `Throwable`. – Marko Topolnik Jan 20 '14 at 19:42
  • one thing, ...so when do you use extends Throwable? – user3126119 Jan 20 '14 at 19:42
  • @MarkoTopolnik I'm familiar with that. Others are not I'm sure :) – Adam Gent Jan 20 '14 at 19:43
  • @user3126119 You never need to, or should, use `extends Throwable` directly. If you create your own exception class (rarely needed), then you'll have `extends RuntimeException` or, possibly, `extends Exception`. – Marko Topolnik Jan 20 '14 at 19:44
  • 1
    @AdamGent I don't know if it does the expected---it doesn't throw the `null` :) – Marko Topolnik Jan 20 '14 at 19:48
  • Well null does extend NPE. It is an oddity. I only pointed it out because I find it amusing.. even though I loathe `null`. – Adam Gent Jan 20 '14 at 19:49
  • 1
    @AdamGent But are you aware that `throw null` actually completes abruptly, failing with a `NullPointerException`? So the `throw` statement itself doesn't get to throw anything. – Marko Topolnik Jan 20 '14 at 19:51
  • @MarkoTopolnik its been awhile since I ran into the use case but I thought for some reason that not to be true. That is the bytecode generated was the same or for some reason throwing `null` was/is more efficient. I can't remember which one but there is actual old Sun code out there that throws null. – Adam Gent Jan 20 '14 at 19:54
  • @AdamGent The bytecode sure says "throw null" but that instruction fails at runtime. It never gets to throw null because it completes abruptly. – Marko Topolnik Jan 20 '14 at 19:55
  • 1
    JLS to the rescue! Section [14.18](http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.18): "If evaluation of the Expression completes normally, producing a null value, then an instance V' of class NullPointerException is created and thrown instead of null. The throw statement then completes abruptly, the reason being a throw with value V'." (That said, a `throw` _always_ completes abruptly, so that's not the reason it never throws null.) – yshavit Jan 20 '14 at 19:58
  • In theory throwing `null` might have been more efficient NPE (as noted by Peter http://stackoverflow.com/a/6591924/318174). I don't recall where I saw it and I can't seem to google the code. – Adam Gent Jan 20 '14 at 20:02
  • 1
    @yshavit That's what I went to check out. It's difficult to talk about normal completion when dealing with the `throw` statement :) So I would say that it completes abruptly squared when you throw a null :) – Marko Topolnik Jan 20 '14 at 20:04
  • @AdamGent No chance it's more efficient. Both instantiate an `NPE`. – Marko Topolnik Jan 20 '14 at 20:06
  • The NPEs in those two cases are for different reasons, @AdamGent. The `i.intValue` one throws NPE because the throws expression completes abruptly (the first bullet point in the part of 14.18 that I quoted above). The second throws NPE because the throws expression completes normally but has a null value (the third bullet point, which I quoted). The stack traces would be different. – yshavit Jan 20 '14 at 20:06
  • I did say *might have been* (Like ancient JDKs) more efficient and I know it results in two different stack traces now. I really have no idea why some one would write `throw null;` but it was in some ancient Sun code. – Adam Gent Jan 20 '14 at 20:10
  • 1
    @AdamGent yshavit is talking about the code in Peter's answer. `throw null` and `throw new NullPointerException()` are completely indistinguishable. – Marko Topolnik Jan 20 '14 at 20:11
0

When a class extends a Throwable, the class is a sub-class of Throwable and you can throw this class as a Throwable anywhere in you code.

class MyCoolThrowable extends Throwable{

  try {
  //... some error ...
  } catch (MyCoolThroable t) {
  //... handle the throwable
  }
}

You add throws Throwable to a constructor or method so that the code that calls your constructor/method will be forced to use try-catch

void mySweetMethod() throws Throwable {
  //...
}

try {
  mySweetMethod();
} catch(Throwable t) {
  //... handle it here
}

I think this clarified what you have read:)

jethroo
  • 2,086
  • 2
  • 18
  • 30
Daniel Nuriyev
  • 635
  • 6
  • 10
  • `Throwable` is not an interface. – Jeffrey Jan 20 '14 at 19:45
  • You can't throw it anywhere in the code -- only from methods that declare `throws Throwable` (which they should almost never do) or in `try` blocks that include a `catch (Throwable t)` (which you should almost never write). If you want to throw it from anywhere, you should extend `RuntimeException`. – yshavit Jan 20 '14 at 20:03
0

I know that each java class should extend the Throwable class so that exceptions can be handled.

No, someone lied to you.

To handle exceptions, you need to keep in mind two things:

  • If it's a checked exception, then the class will refuse to compile if there is not a try...catch block wrapping the dodgy code, or if you don't declare the exception to be thrown.

    try {
        File f = new File("Foo.txt");
    } catch (FileNotFoundException f) {
        System.out.println("File doesn't exist!");
    }
    

    or:

    public void doDodgyStuffWithFile throws FileNotFoundException { }
    
  • If it is an unchecked exception, then you will not get a compile-time failure for not catching the dodgy code, but you may get runtime exceptions from it.

    String input = scanner.nextLine();
    System.out.println(Integer.parseInt(input));
    

If you are throwing or catching the exception, be as specific as possible. Do not give into the temptation to throw/catch Exception, and never catch a Throwable.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230