Skip to content

Commit

Permalink
Remove exception rethrows (CA2200)
Browse files Browse the repository at this point in the history
Also remove redundant try-catches (catching and rethrowing only).
  • Loading branch information
Michael Croes committed Feb 27, 2023
1 parent 4f3e641 commit 94ebcc7
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 243 deletions.
11 changes: 2 additions & 9 deletions main/HPSF/MutableSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,8 @@ public override int Size
{
if (dirty)
{
try
{
size = CalcSize();
dirty = false;
}
catch (Exception)
{
throw;
}
size = CalcSize();
dirty = false;
}
return size;
}
Expand Down
15 changes: 4 additions & 11 deletions main/HSSF/Extractor/EventBasedExcelExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,12 @@ public override String Text
get
{
String text = null;
try
{
TextListener tl = TriggerExtraction();
TextListener tl = TriggerExtraction();

text = tl.text.ToString();
if (!text.EndsWith("\n", StringComparison.Ordinal))
{
text = text + "\n";
}
}
catch (IOException)
text = tl.text.ToString();
if (!text.EndsWith("\n", StringComparison.Ordinal))
{
throw;
text = text + "\n";
}

return text;
Expand Down
8 changes: 4 additions & 4 deletions main/HSSF/Extractor/OldExcelExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ public OldExcelExtractor(FileInfo f)
{
Open(biffStream);
}
catch (IOException e)
catch (IOException)
{
// ensure that the stream is properly closed here if an Exception
// is thrown while opening
biffStream.Close();
throw e;
throw;
}
catch (RuntimeException e)
catch (RuntimeException)
{
// ensure that the stream is properly closed here if an Exception
// is thrown while opening
biffStream.Close();
throw e;
throw;
}
}
public OldExcelExtractor(NPOIFSFileSystem fs)
Expand Down
18 changes: 2 additions & 16 deletions main/HSSF/UserModel/OperationEvaluatorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,7 @@ private static void Add(Hashtable m, Type ptgClass, Type evalClass)
}

ConstructorInfo constructor;
try
{
constructor = evalClass.GetConstructor(OPERATION_CONSTRUCTOR_CLASS_ARRAY);
}
catch (Exception)
{
throw;
}
constructor = evalClass.GetConstructor(OPERATION_CONSTRUCTOR_CLASS_ARRAY);
if (!constructor.IsPublic)
{
throw new Exception("Eval constructor must be public");
Expand Down Expand Up @@ -128,14 +121,7 @@ public static OperationEval Create(OperationPtg ptg)

Object result;
Object[] initargs = { ptg };
try
{
result = constructor.Invoke(initargs);
}
catch (Exception)
{
throw;
}
result = constructor.Invoke(initargs);
return (OperationEval)result;
}
}
Expand Down
4 changes: 2 additions & 2 deletions main/POIFS/Crypt/Standard/StandardDecryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ private int getMoreData()
{
this.obuffer = this.cipher.Update(this.ibuffer, 0, i);
}
catch (Exception ex)
catch (Exception)
{
this.obuffer = null;
throw ex;
throw;
}
this.ostart = 0;
if (this.obuffer == null)
Expand Down
37 changes: 11 additions & 26 deletions main/POIFS/FileSystem/DirectoryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,25 +401,17 @@ public DocumentInputStream CreateDocumentInputStream(string documentName)

public DocumentEntry CreateDocument(NPOIFSDocument document)
{
try
{
DocumentProperty property = document.DocumentProperty;
DocumentNode rval = new DocumentNode(property, this);

((DirectoryProperty)Property).AddChild(property);
DocumentProperty property = document.DocumentProperty;
DocumentNode rval = new DocumentNode(property, this);

_nFilesSystem.AddDocument(document);
((DirectoryProperty)Property).AddChild(property);

_entries.Add(rval);
_byname[property.Name] = rval;
_nFilesSystem.AddDocument(document);

return rval;
_entries.Add(rval);
_byname[property.Name] = rval;

}
catch (IOException ex)
{
throw ex;
}
return rval;
}


Expand Down Expand Up @@ -529,20 +521,13 @@ protected override bool IsDeleteOK

public DocumentEntry CreateDocument(string name, Stream stream)
{
try
if (_nFilesSystem != null)
{
if (_nFilesSystem != null)
{
return CreateDocument(new NPOIFSDocument(name, _nFilesSystem, stream));
}
else
{
return CreateDocument(new OPOIFSDocument(name, stream));
}
return CreateDocument(new NPOIFSDocument(name, _nFilesSystem, stream));
}
catch (IOException ex)
else
{
throw ex;
return CreateDocument(new OPOIFSDocument(name, stream));
}
}

Expand Down
12 changes: 4 additions & 8 deletions main/POIFS/FileSystem/NPOIFSFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,26 +216,22 @@ public NPOIFSFileSystem(FileStream channel, FileInfo srcFile, bool readOnly, boo
//_data = new FileBackedDataSource(channel, readOnly);
ReadCoreContents();
}
catch (Exception)
{
throw;
}
finally
{
if (channel != null)
channel.Close();
}
}
catch (IOException e)
catch (IOException)
{
if (closeChannelOnError && channel != null)
{
channel.Close();
channel = null;
}
throw e;
throw;
}
catch (RuntimeException e)
catch (RuntimeException)
{
// Comes from Iterators etc.
// TODO Decide if we can handle these better whilst
Expand All @@ -245,7 +241,7 @@ public NPOIFSFileSystem(FileStream channel, FileInfo srcFile, bool readOnly, boo
channel.Close();
channel = null;
}
throw e;
throw;
}
}

Expand Down
4 changes: 2 additions & 2 deletions main/POIFS/Macros/VBAMacroReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ private void OpenOOXML(Stream zipFile)
this.fs = new NPOIFSFileSystem(zis);
return;
}
catch (IOException e)
catch (IOException)
{
// Tidy up
zis.Close();

// Pass on
throw e;
throw;
}
}
}
Expand Down
57 changes: 18 additions & 39 deletions main/POIFS/Storage/HeaderBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,13 @@ public class HeaderBlock : HeaderBlockConstants
/// <param name="stream">the source Stream</param>
public HeaderBlock(Stream stream)
{
try
stream.Position = 0;
PrivateHeaderBlock(ReadFirst512(stream));
if (bigBlockSize.GetBigBlockSize() != 512)
{
stream.Position = 0;
PrivateHeaderBlock(ReadFirst512(stream));
if (bigBlockSize.GetBigBlockSize() != 512)
{
int rest = bigBlockSize.GetBigBlockSize() - 512;
byte[] temp = new byte[rest];
IOUtils.ReadFully(stream, temp);
}

}
catch(IOException ex)
{
throw ex;
int rest = bigBlockSize.GetBigBlockSize() - 512;
byte[] temp = new byte[rest];
IOUtils.ReadFully(stream, temp);
}
}

Expand All @@ -130,15 +122,9 @@ public HeaderBlock(ByteBuffer buffer)

public HeaderBlock(byte[] buffer)
{
try
{
PrivateHeaderBlock(buffer);
}
catch (IOException ex)
{
throw ex;
}
PrivateHeaderBlock(buffer);
}

public void PrivateHeaderBlock(byte[] data)
{
_data = data;
Expand Down Expand Up @@ -425,26 +411,19 @@ public POIFSBigBlockSize BigBlockSize

public void WriteData(Stream stream)
{
try
{
new IntegerField(_bat_count_offset, _bat_count, _data);
new IntegerField(_property_start_offset, _property_start, _data);
new IntegerField(_sbat_start_offset, _sbat_start, _data);
new IntegerField(_sbat_block_count_offset, _sbat_count, _data);
new IntegerField(_xbat_start_offset, _xbat_start, _data);
new IntegerField(_xbat_count_offset, _xbat_count, _data);
new IntegerField(_bat_count_offset, _bat_count, _data);
new IntegerField(_property_start_offset, _property_start, _data);
new IntegerField(_sbat_start_offset, _sbat_start, _data);
new IntegerField(_sbat_block_count_offset, _sbat_count, _data);
new IntegerField(_xbat_start_offset, _xbat_start, _data);
new IntegerField(_xbat_count_offset, _xbat_count, _data);

stream.Write(_data, 0, 512);
stream.Write(_data, 0, 512);

for (int i = POIFSConstants.SMALLER_BIG_BLOCK_SIZE; i < bigBlockSize.GetBigBlockSize(); i++)
{
//stream.Write(Write(0);
stream.WriteByte(0);
}
}
catch (IOException ex)
for (int i = POIFSConstants.SMALLER_BIG_BLOCK_SIZE; i < bigBlockSize.GetBigBlockSize(); i++)
{
throw ex;
//stream.Write(Write(0);
stream.WriteByte(0);
}
}

Expand Down
9 changes: 1 addition & 8 deletions main/POIFS/Storage/HeaderBlockWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,7 @@ public static int CalculateXBATStorageRequirements(POIFSBigBlockSize bigBlockSiz
/// </param>
public void WriteBlocks(Stream stream)
{
try
{
_header_block.WriteData(stream);
}
catch (IOException ex)
{
throw ex;
}
_header_block.WriteData(stream);
}

public void WriteBlock(ByteBuffer block)
Expand Down
28 changes: 11 additions & 17 deletions main/POIFS/property/NPropertyTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,21 @@ public void PreWrite()
public void Write(NPOIFSStream stream)
{
Stream os = stream.GetOutputStream();
try

//Leon ByteArrayOutputStream -->MemoryStream
MemoryStream ms = new MemoryStream();
foreach (Property property in _properties)
{
//Leon ByteArrayOutputStream -->MemoryStream
MemoryStream ms = new MemoryStream();
foreach (Property property in _properties)
{
if (property != null)
property.WriteData(os);
}
if (property != null)
property.WriteData(os);
}

os.Close();
os.Close();

// Update the start position if needed
if (StartBlock != stream.GetStartBlock())
{
StartBlock = stream.GetStartBlock();
}
}
catch (System.IO.IOException ex)
// Update the start position if needed
if (StartBlock != stream.GetStartBlock())
{
throw ex;
StartBlock = stream.GetStartBlock();
}
}
}
Expand Down
Loading

0 comments on commit 94ebcc7

Please sign in to comment.