Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7596887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:01:44+00:00 2026-05-30T22:01:44+00:00

Assuming I have a WebCrawler class. There are several errors it can encounter. How

  • 0

Assuming I have a WebCrawler class. There are several errors it can encounter. How should I propagate the errors upward?

Using exceptions:

class WebCrawler
  class UrlBadFormatError < StandardError; end
  class PageNotFoundError < StandardError; end
  class UnauthorizedError < StandardError; end
  def crawl(url)
    if(! url =~ /some_format/)
      raise UrlBadFormatError
    response = get(url)
    if(response.code == 404)
      raise PageNotFoundError
    if(response.code == 403)
      raise UnauthorizedError
    ... 
  end
end

or constants:

class WebCrawler
  URL_BAD_FORMAT = 1
  PAGE_NOT_FOUND = 2
  UNAUTHORZIED = 3 
  def crawl(url)
    if(! url =~ /some_format/)
      return URL_BAD_FORMAT
    response = get(url)
    if(response.code == 404)
      return PAGE_NOT_FOUND
    if(response.code == 403)
      return UNAUTHORZIED
    ... 
  end
end

or symbols:

class WebCrawler
  def crawl(url)
    if(! url =~ /some_format/)
      return :url_bad_format
    response = get(url)
    if(response.code == 404)
      return :page_not_found
    if(response.code == 403)
      return :unauthorized
    ... 
  end
end

which is best? or it depends(on what?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:01:47+00:00Added an answer on May 30, 2026 at 10:01 pm

    For something which indicates programmer error, such as the wrong type of argument passed to a method, definitely throw an exception. The exception will crash the program, drawing the programmer’s attention to the fact that they are using your class incorrectly, so they can fix the problem. In this case, returning an error code wouldn’t make sense, because the program will have to include code to check the return value, but after the program is debugged, such errors shouldn’t ever happen.

    In your WebCrawler class, is it expected that crawl will receive a bad URL as an argument sometimes? I think the answer is probably no. So raising an exception would be appropriate when a bad URL is passed.

    When an exception is raised, the flow of execution suddenly “jumps” to the innermost handler. This can be a useful way to structure code when the exception is not expected to happen most of the time, because you can write the “main flow” of your method as simple, straight-line code without including a lot of details about what will happen when some rare error condition occurs. Those details can be separated from the “main flow” code, and put in an exception handler. When an error condition is expected to happen under normal conditions, though, it can be better to put the error handling code inline with the “main flow”, to make it clearer what is going on. If the control flow of your program “jumps around” (as is the case when exceptions are used for normal flow control), that means the reader also has to “jump around” in the program text as they are figuring out how it works.

    For the other two, I think it is expected that at least sometimes, the HTTP request will return an error code. To determine whether an exception or special return value is the best way to indicate such a condition, I would think about how often those conditions are going to happen under normal usage. Think also about how the client code will read either way. If you use exceptions, they will have to write something like:

    urls.map do |url|
      begin
        crawl(url)
      rescue PageNotFoundError
        ""
      rescue UnauthorizedError
        ""
      end
    end
    

    (By the way, I think this code example shows something: it might be a good idea if both of your custom exceptions inherit from a common superclass, so you can catch both of them with a single rescue clause if desired.) Or if you use error codes, it would look something like:

    urls.map do |url|
      response = crawl(url)
      if [:page_not_found, :unauthorized].include? response
        ""
      else
        response
      end
    end
    

    Which do you think reads better? It’s really up to you. The one thing which you do not want to do is use integer constants for errors. Why use integers? When you print them in a debug trace, you’ll have to go look at the list of constants to see what each one means. And using symbols is just as efficient computationally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have a defined class class MyClass { } Is there a hit
Assuming I have only the class name of a generic as a string in
Assuming I have an open source web server or proxy I can enhance, let's
Assuming I have fonts installed which have the appropriate glyphs in them, is there
Assuming I have a class A as follows: class A{ int id; int getId(){};
Assuming I have a class with a method that takes a System.Linq.Expressions.Expression as a
Assuming I have a schema that describes a root element class Root that contains
Assuming I have a selector for a text node in jQuery, how can I
Assuming you have a constant defined in a class: class Foo { const ERR_SOME_CONST
Assuming I have a class B derived from A A defined foo() and also,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.