• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

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.
0
jedistar
Asked:
jedistar
  • 4
  • 4
  • 3
  • +1
2 Solutions
 
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
0
 
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.
0
 
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?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
jedistarAuthor Commented:
didn't understand that url link though.
0
 
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) {}
0
 
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
0
 
jedistarAuthor Commented:
what abt unchecked, checked and chained exceptions
what are they?
0
 
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.
0
 
CEHJCommented:
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now