?
Solved

Exceptions

Posted on 2006-04-09
12
Medium Priority
?
202 Views
Last Modified: 2013-12-29
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
Comment
Question by:jedistar
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 600 total points
ID: 16411629
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
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 400 total points
ID: 16411729
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
 

Author Comment

by:jedistar
ID: 16411906
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:jedistar
ID: 16412018
didn't understand that url link though.
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16412043
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16412374
>> 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
 

Author Comment

by:jedistar
ID: 16414659
what abt unchecked, checked and chained exceptions
what are they?
0
 
LVL 2

Expert Comment

by:borislavmarkov
ID: 16417525
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16417840
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16518940
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question