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 8173949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:19:36+00:00 2026-06-06T22:19:36+00:00

How would you correct way to handle an exception in this situation? Initially I

  • 0

How would you correct way to handle an exception in this situation? Initially I expect that using a trycatch in the following manner would catch the exception thrown by the helper class when the upload fails.

My aim is for “false” to be returned and a Messagebox to open with the error message I’ll be plucking out of the HTML response from my upload attempt.
I am trying to do this without resorting to bad practise and putting GUI code inside my helper class.

        try
        {
            // returns bool
            successful = UploadHelper.Upload(uploadToPath, File.ReadAllBytes(uploadFromPath), properties);
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }

Helper class:

    public static bool Upload(string webUrl, string documentName, byte[] bytes, Dictionary<string, object> metaInfo, out string result)
    {
        try
        {
            using (WebClient webClient = new WebClient())
            {
                //result is HTML string containing data
                result = Encoding.UTF8.GetString(webClient.UploadData(webUrl + "/_vti_bin/_vti_aut/author.dll", "POST", data.ToArray()));

                // if fails throw exception
                if (result.IndexOf("\n<p>message=successfully") < 0)
                    throw new Exception(result);
            }
        }
        catch (Exception ex)
        {
            result = ex.Message;

            // Upload failed
            return false;
        }
        // Upload succeeded
        return true;
    }

This slightly similar question lead me to thinking I could resolve this issue with a custom exception. However reading this explanation of creating them has confused me as to how they could be used to resolve this situation – as it seems this would just move the Messagebox code to the Exception class which is also not a great place for it.

I am fairly sure this is a situation where I am justified in using a custom exception anyway as the user may be able to resolve the issues depending on the error message returned. So I have written from the aforementioned guide anyway (left out of code example for simplicity).

  • 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-06-06T22:19:38+00:00Added an answer on June 6, 2026 at 10:19 pm

    If you are just going to display the exception’s message in the UI, and your catch block here is just to grab the message, why catch it here at all? If the handling is in the UI, then catch it there (or closer to there) and just grab and display the message at that level.

    You’d generally use exception handling in this sort of case to either grab specific exceptions and extract information from them to report in a particular way (abstract the error message as Charleh comments below), or to drive some sort of automatic retry logic. But there’s absolutely nothing wrong with a method called Upload throwing an exception if it fails to upload something.

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

Sidebar

Related Questions

Would this be the correct way of declaring that an XML element Cluster contains
What would be correct way to create new instance of the class from class
What would be the correct way to create a fully URL-encoded file:// URI from
What would be the correct way to add DISTINCT and/or GROUPBY to ContentResolver -based
suppose i have a sequence, called TEST_SEQ what would be the correct way of
Would it be correct to say that whenever casting is used, the resulting object
Is this code correct? I'm trying to submit it and also I would like
My intuitive response to this question would be ,This is so stupid that I
What is the correct way of detecting and handling an exception in another thread
I have a question about the correct way to handle errors in VBA in

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.