-6

I'm trying to use a try {...} catch on this statement as it's too large and causes an emulator to crash. When it converts it to a string, that is. So the solution is to make a try {...} catch.

public override string ToString()
{ 
   return "[" + Header + "] BODY: " + 
          (PlusEnvironment
             .GetDefaultEncoding()
             .GetString(Body)
             .Replace(Convert.ToChar(0).ToString(), "[0]"));
}

How would I go abouts doing this?

Error:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Plus.Messages.Net.GamePacketParser.handlePacketData(Byte[] Data) in C:\Users\Administrator\Desktop\Emulator\Messages\Net\GamePacketParser.cs:line 75
Date/Time: 2018-02-08 13:27:05,435
Thread: 25
Packet Error!

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at Plus.Communication.Packets.Incoming.ClientPacket.ToString()
   at Plus.HabboHotel.GameClients.GameClient.parser_onNewPacket(ClientPacket 

Message) at Plus.Messages.Net.GamePacketParser.handlePacketData(Byte[] Data)

Someone said:

I specifically recommend wrapping this line Code: Plus.Communication.Packets.Incoming.ClientPacket.ToString() in a try{}catch block and trying to parse the clientpacket to a string, and catching for the memory exception that you get, that way the packets that are too large won't crash the emulator but they will be ignored and not pass through.

Atmazphere
  • 11
  • 2
  • 2
    Split the instruction in variable – Drag and Drop Feb 09 '18 at 12:31
  • 3
    `Convert.ToChar(0).ToString()` is silly. You're using a literal integer and casting it to a char, then to a string. Just use `"0"` and then you don't have to cast. – Flater Feb 09 '18 at 12:35
  • " as it's too large and causes an emulator to crash" Eeeehm, what? Your app won´t crash because a statement is "too large" (whatever this means). It crsahes because you got an exception. Post that exception – MakePeaceGreatAgain Feb 09 '18 at 12:41
  • Why can't you wrap this statement in a try block and in the catch block return whatever your error return value is? – bashrc Feb 09 '18 at 12:42
  • 1
    Using a try/catch here isn't a solution. At best it's a crude workaround, at worst, it *hides* the actual problem. Find out what *that* is and solve it. – Corak Feb 09 '18 at 12:43
  • With the lack of basic understanding of try catch you seem unable to read and understand the error message. May you provide more information about the crash you are talking about. – Drag and Drop Feb 09 '18 at 12:44
  • Too long to add -> https://hastebin.com/ejinuxihol.sql – Atmazphere Feb 09 '18 at 12:45
  • @Atmazphere - `NullReferenceException` now we're getting somewhere. That's not too much to add... see https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it for further information. – Corak Feb 09 '18 at 12:49
  • "So the solution is to make a try {...} catch" No, it´s not. The solution is to *avoid* this error. Anyway please post the error you get **into your question**. – MakePeaceGreatAgain Feb 09 '18 at 12:50
  • Obviously the value of `Body` is too large causing an `OutOfMemoryException`. Instead of handling this exception you should try to avoid it by checking the number of characters within `Body`. If it exceeds a given maximum, then you may raise your own exception. – MakePeaceGreatAgain Feb 09 '18 at 12:57
  • Where did you get this suggestion? It´s a bad idea IMHO, but maybe there are some good reasons to do so. – MakePeaceGreatAgain Feb 09 '18 at 13:01
  • @Flater `Convert.ToChar(0).ToString()` is not "0", it's string with NUL character ("\0"). – Evk Feb 09 '18 at 13:35

1 Answers1

-1

You can decalre a variable before the return statement:

public override string ToString()
{ 
  try 
  {

     var str = "[" + Header + "] BODY: " + 
        (PlusEnvironment
           .GetDefaultEncoding()
           .GetString(Body)
           .Replace(Convert.ToChar(0).ToString(), "[0]"));

    // return if no exception is thrown
    return str;
  }
  catch (System.Exception e)
  {
    return e.Message:
  }

}

HeW
  • 448
  • 2
  • 12