constructors and exception handling

A

Angus Parvis

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 does not have to be
caught. Why that?

I'm curious about the answer ;)

Greetings,

Angus
 
T

Tor Iver Wilhelmsen

Angus Parvis said:
But when a constructor throws an exception, it does not have to be
caught. Why that?

It does (example: java.net.URL). Why do you think it doesn't have to?
Do you have an example?

Remember that RuntimeException and its subclasses need to be caught
even if you declare them in "throws".
 
A

Angus Parvis

Tor said:
It does (example: java.net.URL). Why do you think it doesn't have to?
Do you have an example?

I've tried it with java.net.URL and you're right. Uhm, maybe I've
confused something. But the code that made me think this is at home, not
here at work. I'll have a closer look and post my results ;)
 
S

Sebastian Scheid

Hi,

Tor Iver Wilhelmsen said:
It does (example: java.net.URL). Why do you think it doesn't have to?
Do you have 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 doesn't need to declare a
RuntimeException to be thrown even if it does not catch it itself. Probably
Angus throws a RuntimeException in the constructor.

Sebastian
 
T

Tor Iver Wilhelmsen

Sebastian Scheid said:
That's wrong.

Yes, I think I was a bit tired when I wrote it: Runtime exceptions
need NOT be caught, even if you declare them in "throws".
 
A

Angus Parvis

Sebastian said:
That's wrong.
It's the other way around: a method doesn't need to declare a
RuntimeException to be thrown even if it does not 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 didn't 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 Doc says:

"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."

I have to say, I don't 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
 
T

Tor Iver Wilhelmsen

Angus Parvis said:
I have to say, I don't 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 will hate C#, where ALL exceptions are like Java's
RuntimeExceptions. :) No checked exceptions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top