Exceptions

I would like to know what would be the diff between using try/catch blocks and

using: "throw new exception"

    private void calculateSquareRootWithException(double input)
          throws IllegalArgumentException {
        if (input < 0.0) {
            throw new IllegalArgumentException("The input must be >= 0 : input = " + input);

2. For the above is it neccessary to add "throws IllegalArgumentException" in the private void line?

thanks.
jedistarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
1. They're not mutually exclusive. You could catch the exception thrown in that method, but normally you don't catch RuntimeException
2. No

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hoomanvCommented:
2. A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
jedistarAuthor Commented:
Thanks,

What does it mean by mutually exclusive?

How do i catch RuntimeException? I thought the above was also part of runtime.
What causes runtime ex?
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

jedistarAuthor Commented:
didn't understand that url link though.
hoomanvCommented:
what didn't you understand in it ?

> What causes runtime ex?
that url would answer most of your Qs

> How do i catch RuntimeException
just as you catch any other Exception
   catch(RuntimeException e) {}
CEHJCommented:
>> How do i catch RuntimeException

As i mentioned above:

>>normally you don't catch RuntimeException

since RuntimeException is often often irrecoverable. There are exceptions (no pun intended) to this though
jedistarAuthor Commented:
what abt unchecked, checked and chained exceptions
what are they?
borislavmarkovCommented:
In Java there are 2 types of exceptions - checked and unchecked exceptions.
Checked exceptions have to be declared in throws clause of method declaration.
Unchecked exceptions does not have to declared but this is not forbidden for clarification and better view of code.

IllegalArgumentException is from type unchecked exceptions.

Unchecked means in compile time java would not make trouble if you declare it or not.

That is why it is not necessary to declare when you throw "IllegalArgumentException".

public class IllegalArgumentException extends RuntimeException{...}

all RuntimeExceptions are not checked but hey can be catched of course if you like to handle them.
CEHJCommented:
CEHJCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.