constructors and exception handling
11 Message(s) by 4 Author(s) originally posted in java developer
| From: Angus Parvis |
Date: Wednesday, July 28, 2004
|
Hi,
When a
method throws an exception, I always have to deal with it in some
way (let it fall through or catch it).
But when a
constructor throws an exception, it doesn't have to be
caught. Why that?
I'm curious about the answer ;)
Greetings,
Angus
| From: Angus Parvis |
Date: Thursday, July 29, 2004
|
wrote in
message :
Hi,
When a method throws an exception, I always have to deal with it in some
way (let it fall through or catch it).
But when a constructor throws an exception, it doesn't have to be
caught. Why that?
I'm curious about the answer ;)
Greetings,
Angus
LAPSED
| From: Angus Parvis |
Date: Thursday, July 29, 2004
|
Hi,
When a method throws an exception, I always have to deal with it in some
way (let it fall through or catch it).
But when a constructor throws an exception, it doesn't have to be
caught. Why that?
I'm curious about the answer ;)
Greetings,
Angus
| From: Tor Iver Wilhelmsen |
Date: Thursday, July 29, 2004
|
Angus Parvis
<angusparvis@xxxxxxxxxxx> writes:
But when a constructor throws an exception, it doesn't have to be
caught. Why that?
It does (example: JAVA.net.URL). Why do you think it does not have to?
Do you've an example?
Remember that RuntimeException and its
subclass es need to be caught
even if you declare them in "throws".
| From: Angus Parvis |
Date: Thursday, July 29, 2004
|
wrote in message:
It does (example: JAVA.net.URL). Why do you think it does not have to?
Do you've an example?
I've tried it with JAVA.net.URL and you're right. Uhm, maybe I have
confused something. But the
code that made me think this is at home, not
here at work. I will have a closer look and post my results ;)
| From: Sebastian Scheid |
Date: Thursday, July 29, 2004
|
Hi,
"Tor Iver Wilhelmsen"
<tor.iver.wilhelmsen@xxxxxxxxxxx> schrieb im
Newsbeitrag
Angus Parvis <angusparvis@xxxxxxxxxxx> writes:
> But when a constructor throws an exception, it doesn't have to be
> caught. Why that?
It does (example: JAVA.net.URL). Why do you think it does not have to?
Do you've an example?
Remember that RuntimeException and its subclasses need to be caught
even if you declare them in "throws".
That's wrong.
It's the other way around: a method does not need to declare a
RuntimeException to be thrown even if it doesn't catch it itself. Probably
Angus throws a RuntimeException in the constructor.
Sebastian
| From: Tor Iver Wilhelmsen |
Date: Thursday, July 29, 2004
|
"Sebastian Scheid"
<sebastian_scheid@xxxxxxxxxxx> writes:
That's wrong.
wrote in message it: Runtime exceptions
need NOT be caught, even if you declare them in "throws".
| From: Angus Parvis |
Date: Thursday, July 29, 2004
|
wrote in message:
That's wrong.
It's the other way around: a method does not need to declare a
RuntimeException to be thrown even if it doesn't catch it itself. Probably
Angus throws a RuntimeException in the constructor.
Sebastian
No, I always subclass JAVA.lang.Exception. Not for a special reason, I
simply did not know that a JAVA.lang.RuntimeException does have to be
caught or declared to be thrown. But maybe at that
point I used some
sublass of RuntimeException.
That's what the
API (application
programming interface)Doc says:
"A method isn't required to declare in its throws
clause any subclasses
of RuntimeException that might be thrown during the
execution of the
method but not caught."
I've to say, I do not like this - I'd say, that it's always better to
be aware of the exceptions that can be thrown and to
handle them
appropriately.
What's your opinion? Any hints, in what cases it is better to throw a
subclass of RuntimeException and in what cases subclasses of Exception?
Angus
| From: Tor Iver Wilhelmsen |
Date: Thursday, July 29, 2004
|
Angus Parvis
<angusparvis@xxxxxxxxxxx> writes:
I have to say, I do not like this - I'd say, that it's always better to
be aware of the exceptions that can be thrown and to handle them
appropriately.
Then you'll hate C#, where ALL exceptions are like JAVA's
RuntimeExceptions. :) No checked exceptions.
| From: Angus Parvis |
Date: Thursday, July 29, 2004
|
wrote in message:
Then you'll hate C#, where ALL exceptions are like JAVA's
RuntimeExceptions. :) No checked exceptions.
I heard of that. What a nightmare ;).
| From: Roedy Green |
Date: Thursday, July 29, 2004
|
Next Message: executing remote command
Blogs related to constructors and exception handling
1. What are the four corner stones of OOP ? Abstraction ...
What is
constructor chaining and how is it achieved in
Java ? A child object
constructor always first needs to construct its parent (which in turn calls its
... 155 What will happen to the
Exception object after
exception handling ...
JAVA BASIC QUESTIONS
The
Java interpreter is used for the execution of the source code.
... 14)
Constructors can be overloaded like regular methods. a)True b)False
... 7) What will happen to the
Exception object after
exception handling?
...
Sun, are you listening?
The unfortunately named Hardcore
Java book is actually a pretty good read.
... If you're really serious about not dealing with pesky
exception handling,
... They haven't gotten back to me on my int Enum
constructors, though.
...
Java enhancements I would like to see
Those extensions complement
Java in useful ways to make some often used
... We all know how annoying correct
exception handling and proper closing is in some cases.
... I can won't elaborate here on the details (
constructor handling,
...
Intevrview Questions
What's the difference between standard
JAVA array and ArrayList
... What are the situations you will need a
constructor to be private? Can you explain final modifier?
... Can you explain in short how
JAVA exception handling works?
...
Writing Software - Part 1
In
Java, goto is a reserved word, but it is not implemented.
... And if we’re already on the subject of
exception handling, let me tell you
... Naturally, it means that
constructors are not allowed to do any work (so there’s always a
...