473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

what exceptions do abstract classes (WebRequest) throw?

I know that WebRequest.GetResponse can throw WebException from
internet tutorials. However in the MSDN docs:
http://msdn2.microsoft.com/en-us/lib...tresponse.aspx
It only lists NotImplementedException in the Exceptions section.
(Note that it does mention WebException in the Remarks section, but
who knows if this is always the case for such classes, and thus
perhaps they can't be trusted to always list these, and even if it is
trustworthy, why isn't in mentioned in the Exceptions section? It IS
an exception, after all.)

So, I want to call WebResponse.GetResponseStream, and how am I
supposed to know what exceptions it may throw?
http://msdn2.microsoft.com/en-us/lib...eststream.aspx
I'm betting it throws WebException, but no where is it mentioned that
it does.

Examples on the internet use the WebRequest class directly. But,
should I (and they) be using one of the derived classes directly? And
thus, I assume, those classes' MSDN docs would list what exceptions
they throw? How can anyone use an abstract class without knowing
about what exceptions are possibly thrown? That sounds like a
horrible programming practice to me.

If I know I will be always using HTTP, then there's no reason to not
exclusively use HttpWebRequest, instead of the abstract WebRequest,
and I bet its docs show what exceptions it may throw. Hm, no, wait,
it says: "Do not use the HttpWebRequest constructor. Use the
System.Net.WebRequest.Create method to initialize new HttpWebRequest
objects." so you really should use WebRequest unless there's a reason
not to.

This is just confusing... do people just not care that things can
throw exceptions? We're talking about internet connections which are
not exaclty infallible. We should handle the exceptions that are
likely to happen.

Zytan

Mar 21 '07 #1
2 2944
Zytan,

See inline:
So, I want to call WebResponse.GetResponseStream, and how am I
supposed to know what exceptions it may throw?
The documentation for the derived class that implements the
functionality of GetResponseStream should indicate this. I would say that
if there are known specific exceptions that can be thrown in situations, it
should be documented.
http://msdn2.microsoft.com/en-us/lib...eststream.aspx
I'm betting it throws WebException, but no where is it mentioned that
it does.
Nor should it. You are looking at the documentation for the base class.
Trying to list out all the exceptions that a method can throw becomes an
exercise in futility due to the large number of exceptions that can be
thrown anywhere due to factors outside of the programmers control (out of
memory exceptions, thread abort exceptions, and THEN you have any exceptions
that a derived class can throw, which is up to the person implementing the
derived class).
Examples on the internet use the WebRequest class directly. But,
should I (and they) be using one of the derived classes directly? And
thus, I assume, those classes' MSDN docs would list what exceptions
they throw? How can anyone use an abstract class without knowing
about what exceptions are possibly thrown? That sounds like a
horrible programming practice to me.
If you know that you are going to be working with a specific protocol,
then by all means, use that specific class. However, if you are going to
use generic functionality that is applicable to many implementations of the
base class, and protocol is not an issue, use a variable of the type of the
base class.
If I know I will be always using HTTP, then there's no reason to not
exclusively use HttpWebRequest, instead of the abstract WebRequest,
and I bet its docs show what exceptions it may throw. Hm, no, wait,
it says: "Do not use the HttpWebRequest constructor. Use the
System.Net.WebRequest.Create method to initialize new HttpWebRequest
objects." so you really should use WebRequest unless there's a reason
not to.
Interesting, when I look at the documentation for the GetRequestStream
and GetResponse methods, there is a nice list of specific exceptions that
can be thrown and the situations that they will be thrown in.

The quote you have has nothing to do with exceptions, but rather, how
you should create the specific implementation of the HttpWebRequest. Yes,
you should use the static create method to initialize new HttpWebRequest
objects, as the constructor does not do the specific work, and the model for
creating WebRequest-derived classes is a factory model. If you need to work
with the specific type, then cast to the specific type.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
Mar 21 '07 #2
So, I want to call WebResponse.GetResponseStream, and how am I
supposed to know what exceptions it may throw?

The documentation for the derived class that implements the
functionality of GetResponseStream should indicate this. I would say that
if there are known specific exceptions that can be thrown in situations, it
should be documented.
Yes, I predicted that the derived classes would show this information,
and they do. So, the problem is: If I must use the base abstract
class, how am I to know which of the derived classes will be invoked?
In the case of WebResponse, it could be HttpWebResponse or
FileWebResponse. So, I basically have to look at all of them to know
which exceptions each of them may throw.
http://msdn2.microsoft.com/en-us/lib...request.getreq...
I'm betting it throws WebException, but no where is it mentioned that
it does.

Nor should it. You are looking at the documentation for the base class.
Trying to list out all the exceptions that a method can throw becomes an
exercise in futility due to the large number of exceptions that can be
thrown anywhere due to factors outside of the programmers control (out of
memory exceptions, thread abort exceptions, and THEN you have any exceptions
that a derived class can throw, which is up to the person implementing the
derived class).
Right. But consider that these aren't classes I programmed. And
HttpWebRequest even recommends to use the base abstract class for
construction. And the derived classes appear to throw exceptions that
'look' like the base abstract class - WebException (not
HttpWebException or FileWebException), so it the class was designed
such that it 'appears' you are using a 'normal' class, not an abstract
class. So, why can't it tell me information about the derived
classes?

But, I guess you're right in that the format of MSDN shows only
exceptions that the class you are looking at throws, which makes
sense, since otherwise, there's dependencies. So, it shouldn't be
mentioned under the Exceptions section, I agree.

But, it still seems it really should in the Remarks section. I just
fail to see how anyone is supposed to know about all these possible
exceptions without looking into every derived class. I guess that's
the only solution, though.

Oh wait! For WebRequest.GetRequestStream it does! :) So, someone
agrees with me.
http://msdn2.microsoft.com/en-us/lib...eststream.aspx
"The WebRequest class is an abstract class. The actual behavior of
WebRequest instances at run time is determined by the descendant class
returned by the System.Net.WebRequest.Create method. For more
information about default values and exceptions, see the documentation
for the descendant classes, such as HttpWebRequest and
FileWebRequest."

The docs for WebResponse.GetResponseStream just aren't so nice.
If you know that you are going to be working with a specific protocol,
then by all means, use that specific class. However, if you are going to
use generic functionality that is applicable to many implementations of the
base class, and protocol is not an issue, use a variable of the type of the
base class.
Right, and I agree. Except that HttpWebRequest exclaims that I MUST
use the base abstract class! This means that I could get any of the
derived classes as a result. Life would be easier if I could just say
I wanted to use HTTP only.
Interesting, when I look at the documentation for the GetRequestStream
and GetResponse methods, there is a nice list of specific exceptions that
can be thrown and the situations that they will be thrown in.
Yes, for the derived classes, I know, and I expected that. That
wasn't the issue. My issue was when I am using the abstract class,
how can I know which exceptions may appear? I had no idea even what
classes were derived from it. Some of the abstract methods do mention
their derivatives, though, which is nice. I wish they all did.
The quote you have has nothing to do with exceptions, but rather, how
you should create the specific implementation of the HttpWebRequest. Yes,
you should use the static create method to initialize new HttpWebRequest
objects, as the constructor does not do the specific work, and the model for
creating WebRequest-derived classes is a factory model. If you need to work
with the specific type, then cast to the specific type.
Yes, but using the base constructor means I could end up with any of
the derived classes, so I must be prepared for all of them. I would
rather just use the derived class that I need.
Hope this helps.
Yes, it does, thanks for your time, Nicholas.

Zytan

Mar 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Simon Harvey | last post by:
Hi, Does anyone know an easy way to identify the exceptions that a class can potentially throw. I was looking for a list of the exceptions that the Hashtable class can potentially provide,...
8
by: Vishal Gandhi | last post by:
Hi , Please help me by advising an real life scenario where Abstract Classes should be used over Interfaces or vice versa . Whats the basic difference between Abstract Class and interface other...
12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
4
by: N.RATNAKAR | last post by:
hai, what is abstract class and abstract method
4
by: Eric | last post by:
I was wondering what people thought about the information found at: http://g.oswego.edu/dl/mood/C++AsIDL.html Specifically, I am interested in the following recommendation: ---- Since...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
1
by: Swathika | last post by:
Hi, Sometimes, you never get chance to learn 'Advanced Design Concepts and Real-time Scenarios' from institutes or through book-learning. But, in my blog, I have gathered some of the amazing...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.