1

I am a little bit confused with exceptions in Java and when to use which particular style of implementation.

I used IllegalArgumentException as an example, but the main point I would like to address is when does one throw, extends or throw new exception?

Also as an additional point I have an assignment where I have to create a java class and the spec vaguely states that the constructor should throw an IllegalArgumentException so which one would be the best to use?

public class Test{

    //when does one use this type of exception
    public Test(String yourName) throws IllegalArgumentException{
      //code implemented
    }

    //when does one use this type of exception
    public Test(String yourName) extends IllegalArgumentException{
      //code implemented
    }

    public Test(String yourName){

         if(yourName.length() <= 0){
             //why not use this type of exception instead 
             //and what happens when I use this type of exception
             throw new IllegalArgumentException("Please Enter Your Name..!");
         }
    }

}

Thanks in advance.

user866190
  • 855
  • 5
  • 14
  • 31

3 Answers3

1

When some Exception occurs, you have two ways of handling it: doing throws from the method or doing try-catch. The first one looks like this:

public class MyClass {
    public void myMethod() throws IllegalArgumentException {
        someOtherMethod();
    }
}

In this case you know that someOtherMethod() can throw an exception and you don't want to handle it - you just pass it further. After that, the invoker of myMethod() should take care of the Exception.

But the second way is when you handle it by yourself:

public void myMethod()  {
    try {
        someOtherMethod();
    } catch (Exception e) {
        System.out.println("You've got an exception!");
    }
}

About throwing exceptions manually - you may suppose that you do it in someOtherMethod(). When you do throw new IllegalArgumentException("Please Enter Your Name..!"); the program stops with a message about this exception (unless you handle it in a try-catch way).

And at last, you extend some exception, when you create your own Exception class:

class MyException extends IllegalArgumentException {
    ...
}

In this case you may do throw new MyException(); in your code.

I'd advise you to read more about exceptions in Java to understand what is going on. You may start with this lesson.

Scadge
  • 9,380
  • 3
  • 30
  • 39
  • Never ever use "throws Exception" - unless you're writing unit tests. – Sami Korhonen Sep 19 '13 at 20:01
  • Could you please give some explanation? Never heard of it. – Scadge Sep 19 '13 at 20:09
  • Ah, that's what you mean. I wrote it just for simplicity of example, sure it should be something like `throws IllegalArgumentException`. But thanks, I edit it not to confuse anyone. – Scadge Sep 19 '13 at 20:21
0

To ensure that you don't end up creating exceptions which already have equivalent in the standard library, I normally have a peek at the documentation before creating new exceptions. Also, it's very easy to go crazy with really big exception hierarchies if you are not careful. Don't create new exceptions just because you think you need to throw one somehow; create one because a code somewhere down the call stack would be doing something useful/different with that exception.

public Test(String yourName) throws IllegalArgumentException

You normally never specify runtime exception in the throws clause though it might be helpful if you need this information to be part of the public API.

public Test(String yourName) extends IllegalArgumentException

This doesn't look right and isn't valid Java.

Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
  • No, you specify *NEVER* the throws with a RuntimeException. You have to document it in your JavaDoc block. – Michael-O Sep 19 '13 at 18:55
  • @Michael-O: The standard library has many such instances; for e.g. take a look at `parseInt` from `Integer` class. Though I agree that specifying in the Javadoc should be enough. – Sanjay T. Sharma Sep 19 '13 at 19:00
  • unfortunately a lot of old code is broken in the JDK these days and they cannot be changed or removed. You should read Joshua Bloch's [book](http://books.google.de/books?id=ka2VUBqHiWkC&lpg=PP1&hl=de&pg=PA252#v=onepage&q&f=false) on that. – Michael-O Sep 19 '13 at 20:01
0

I would only create a new exception type when you need to. You need a new type when you expect the caller to have catch clause for your new exception.

you can create new exceptions just to be more descriptive but that is what I use the message for.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130