Skip to main content
added 662 characters in body
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188

youYou need to get rid of these try/catch statements, they are everywhere.

insteadInstead of what you have


Don't use try/catch statements around using statements, there really is no need to do that, except in an instance like this where you want an exception to alter the output of the function like this. the only issue with doing this, is that the caller doesn't know why the conversion failed because the exception was caught and "handled".

As far as using a using statement around IDisposable objects, put all of them in using blocks, so if there is an exception or the application finishes correctly the object is disposed of properly and there is no memory leak. you don't even have to close the IDisposable object if you don't want to.

you need to get rid of these try/catch statements, they are everywhere.

instead of what you have

You need to get rid of these try/catch statements, they are everywhere.

Instead of what you have


Don't use try/catch statements around using statements, there really is no need to do that, except in an instance like this where you want an exception to alter the output of the function like this. the only issue with doing this, is that the caller doesn't know why the conversion failed because the exception was caught and "handled".

As far as using a using statement around IDisposable objects, put all of them in using blocks, so if there is an exception or the application finishes correctly the object is disposed of properly and there is no memory leak. you don't even have to close the IDisposable object if you don't want to.

Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188

you need to get rid of these try/catch statements, they are everywhere.

Use one Try Catch Statement

instead of what you have

public static bool ConvertToBase64(string inputFile, string outputFile)
{
    try
    {
        using (FileStream inputFileStream = new FileStream(inputFile, FileMode.Open), 
               outputFileStream = new FileStream(outputFile, FileMode.Create))
        {
            try
            {
                ToBase64Transform base64Transform = new ToBase64Transform();
                //Buffers for read/write operations
                Byte[] outputBuffer = new byte[base64Transform.OutputBlockSize];
                Byte[] inputBuffer = new byte[inputFileStream.Length];
                //Offset to count the number of bytes transformed so far
                int inputOffset = 0;
            
                try
                {
                    inputFileStream.Read(inputBuffer, 0, (int)inputFileStream.Length);
                }
                catch(Exception ex)
                {
                    return false;
                }
                
                if (false == base64Transform.CanTransformMultipleBlocks)
                {
                    while (inputBuffer.Length - inputOffset > base64Transform.InputBlockSize)
                    {
                        //Transform a block of input data
                        base64Transform.TransformBlock(inputBuffer, inputOffset, inputBuffer.Length - inputOffset, outputBuffer, 0);
                        inputOffset += base64Transform.InputBlockSize;
                    
                        try
                        {
                            outputFileStream.Write(outputBuffer, 0, base64Transform.OutputBlockSize);
                        }
                        catch(Exception ex)
                        {
                            return false;
                        }
                            
                        //Insert a new line after 76 characters
                        if (inputOffset % 19 == 0)
                        {
                            byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
                            outputFileStream.Write(newline, 0, newline.Length);
                        }
                    }
                }
            
                //Final block transform
                try
                {
                    byte[] lastBlock = base64Transform.TransformFinalBlock(inputBuffer, inputOffset, inputBuffer.Length - inputOffset);
                    outputFileStream.Write(lastBlock, 0, base64Transform.OutputBlockSize);
                }
                catch(Exception ex)
                {
                    return false;
                }
            
                if (false == base64Transform.CanReuseTransform)
                {
                    base64Transform.Clear();
                }
            }
            catch(Exception ex)
            {
                return false;
            }
        }
    }
    catch(Exception ex)
    {       
        return false;
    }

    return true;
}

Only Catch when you need to catch. Like this

public static bool ConvertToBase64(string inputFile, string outputFile)
{
    try
    {
        using (FileStream inputFileStream = new FileStream(inputFile, FileMode.Open), 
                outputFileStream = new FileStream(outputFile, FileMode.Create))
        {
            ToBase64Transform base64Transform = new ToBase64Transform();
            //Buffers for read/write operations
            Byte[] outputBuffer = new byte[base64Transform.OutputBlockSize];
            Byte[] inputBuffer = new byte[inputFileStream.Length];
            //Offset to count the number of bytes transformed so far
            int inputOffset = 0;
        
                inputFileStream.Read(inputBuffer, 0, (int)inputFileStream.Length);
            
            if (false == base64Transform.CanTransformMultipleBlocks)
            {
                while (inputBuffer.Length - inputOffset > base64Transform.InputBlockSize)
                {
                    //Transform a block of input data
                    base64Transform.TransformBlock(inputBuffer, inputOffset, inputBuffer.Length - inputOffset, outputBuffer, 0);
                    inputOffset += base64Transform.InputBlockSize;
                
                    outputFileStream.Write(outputBuffer, 0, base64Transform.OutputBlockSize);
                    
                        
                    //Insert a new line after 76 characters
                    if (inputOffset % 19 == 0)
                    {
                        byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
                        outputFileStream.Write(newline, 0, newline.Length);
                    }
                }
            }
        
            //Final block transform
            byte[] lastBlock = base64Transform.TransformFinalBlock(inputBuffer, inputOffset, inputBuffer.Length - inputOffset);
            outputFileStream.Write(lastBlock, 0, base64Transform.OutputBlockSize);
            
            if (false == base64Transform.CanReuseTransform)
            {
                base64Transform.Clear();
            }
        }
    }
    catch(Exception ex)
    {       
        return false;
    }
    return true;
}

If there is an exception let it bubble up to where you want to catch it, not where it happens, you aren't even keeping the stack trace, so it doesn't matter where you catch it. so just do one try/catch statement here.