1. I am trying to connect to the server via HttpClient

    try { HttpClient httpclient = new HttpClient(); var result = await httpclient.GetStringAsync("Адрес"); dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(result); Temperature = x.main.temp; } 
  2. In the line "Address" I write the wrong way to raise and catch an exception ...

      catch (Exception httpex) { throw new HttpException("Не удалось выполнить подключение к узлу", httpex.InnerException, httpex.Source); } 
  3. I pass the information to my own class HttpException , where I call the method (and pass parameters to it), which writes to the xml file.

     [Serializable] public class HttpException : Exception { public HttpException() { } public HttpException(string message) : base(message) { } public HttpException(string message, Exception inner, string source) :base(message, inner) { XmlLog.Write(message, inner, source); } protected HttpException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } 

So here. After writing to the xml file, the statement returns to the line where the exception was initialized.

 throw new HttpException("Не удалось выполнить подключение к узлу", httpex.InnerException, httpex.Source); 

And then it throws me an exception, where it says that my own "HttpException" exception was not handled by the user. I just can not understand where I make a mistake. Judging by the debugging, everything that happens in my class (HttpException), including working with xml, happens without errors.

  • And you put breakpoint on catch and look. And also, add a catch /*без параметров*/ { /* и breakpoint вот здесь */ } to the tail catch /*без параметров*/ { /* и breakpoint вот здесь */ } - VladD
  • 3
    Exactly what you asked for. throw new HttpException(...) = выбросить новое исключение_имеющее_класс_HttpException - 4per 10:51 pm
  • VladD: not quite caught the idea. If possible, explain the purpose. 4per: Well, how then does it "correctly" handle exceptions with its own class generation? - AlfredBauer
  • 2
    Here you write ... чтобы вызвать и словить исключение ... Then there is an excerpt of the text of the program in which you really have to вызвать , but where is the словить ? If you don’t have a словить , it means that it’s runtime and will correctly report that he had to catch him instead of the user. - Sergey

1 answer 1

To handle the exception, use the try-catch construct that you used.

The word throw is used to generate an exception.

This is what happens in your code:

After an exception is thrown in the try block, control passes to the catch , where you manually generate a new, unhandled exception.

(Because there is no next try construct inside the catch block. It’s not there, in principle, because it’s not necessary to write an exception handler in an exception handler)

If I understand your goal correctly, then you want to write something like a logger that will add an error message to the xml / output_on_screen / send_to the network.

Here is an example of an elementary logger; you can further modify it to the best of your promiscuity. :)

 using System; namespace ConsoleApplication3 { public static class Logger { public static void Log(string message) { Console.WriteLine(message); } public static void Log(Exception ex) { Console.WriteLine(ex.Message); } } class Program { static void Main(string[] args) { try { throw new Exception("pew pew"); } catch (Exception e) { Logger.Log(e); } } } } 

So I think that you can find many options for ready loggers on the Internet, one of which will suit you for sure.