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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:55:50+00:00 2026-06-02T10:55:50+00:00

try { ///// here is the code that might throw erros. If I discover

  • 0
try
{
    ///// here is the code that might throw erros. If I discover the user is unauthorized I throw a WebFaultException myself to alert the client
}      
catch (WebFaultException ex)
{
    throw ex; //but if I wrap all code in try-catch, I have to rethrow the exception o the status code reaches the client
}
catch (Exception ex)
{
    throw new WebFaultException(ex.Message, HttpStatusCode.InternalServerError);
}

Should I wrap all in a try-catch, or what do you recommend? I use WCF with rest-ful JSON services

  • 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-02T10:55:51+00:00Added an answer on June 2, 2026 at 10:55 am

    you could do, but it would probably be better to implement IErrorHandler and add it as a behaviour to your service, which will allow your unhandled exceptions to be handled in a single place, so you’ll be able to create a fault exception there to return details to the users.

    ErrorHandler : IErrorHandler
    {
    ... just implement the handling of errors here, however you want to handle them
    }
    

    then to create a behaviour which uses this:

    /// <summary>
    ///   Custom WCF Behaviour for Service Level Exception handling.
    /// </summary>
    public class ErrorHandlerBehavior : IServiceBehavior
        {
        #region Implementation of IServiceBehavior
    
    
        public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
            }
    
    
        public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
                                          Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
            {
            }
    
    
        public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
            IErrorHandler errorHandler = new ErrorHandler ();
    
            foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
                {
                var channelDispatcher = channelDispatcherBase as ChannelDispatcher;
    
                if (channelDispatcher != null)
                    {
                    channelDispatcher.ErrorHandlers.Add (errorHandler);
                    }
                }
            }
    
        #endregion
        }
    

    Then if you are self hosting you can just add the behaviour programmatically:

     myServiceHost.Description.Behaviors.Add (new ErrorHandlerBehavior ());
    

    if you want to add it via configuration then you need one of these:

    public class ErrorHandlerElement : BehaviorExtensionElement
        {
        public override Type BehaviorType
            {
            get { return typeof (ErrorHandlerBehavior); }
            }
    
        protected override object CreateBehavior ()
            {
            return new ErrorHandlerBehavior ();
            }
        }
    }
    

    and then the configuration:

    <system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="ErrorLogging" type="ErrorHandlerBehavior, ErrorHandling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<whatever>" />
      </behaviorExtensions>
    </extensions>
    <bindings>
      <basicHttpBinding>
        <binding name="basicBinding">
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="Service1Behavior" name="Service">
        <endpoint address="" binding="basicHttpBinding"  bindingConfiguration="basicBinding" contract="Service" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Service1Behavior">
          <serviceMetadata httpGetUrl="" httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <ErrorLogging />  <--this adds the behaviour to the service behaviours -->
        </behavior>
      </serviceBehaviors>
    </behaviors>
    

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

Sidebar

Related Questions

I have this code try { //AN EXCEPTION IS GENERATED HERE!!! } catch {
here is the code private ResultSet rsResult; try { rsResult = DBProcess.statement.executeQuery(SELECT * FROM
Here is the code currently used. public String getStringFromDoc(org.w3c.dom.Document doc) { try { DOMSource
I try to us wp_enqueue_script to load my javascript, here is my code: <?php
I'm trying to make things simpler. Here is my code: If Threading.Monitor.TryEnter(syncRoot) Then Try
Here's the error code: The app you are using is not responding. Please try
It's pretty difficult to show code for ASP.NET here, so I will try my
I was hoping I might get some help here so that I might finally
As I don't know how to ask it to google, I'll try here. I
I've asked at github , but will try asking here as well. Nothing wrong

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.