Suppressed Exception On The Empty Catch Block

When you investigate an issue or a problem you usually get the error or exception message first and then try to replicate that exception in a lower environment such as your local machine. What if there was an error but the application did not produced a message or even a trace where the error occurred? Big problem. One such case to avoid this problem and to avoid passing this problem to the next person that will support your application, is to avoid having suppressed exception on the empty catch block on your code. Yes, having an empty catch block will not be detected by the C# compiler, bummer.

The culprit

public class SilentExceptionSample
{

public void DivideOperation(int a, int b)
{
try
{
var result = a / b;
}
catch (Exception)
{
//throw;
}

}

}

The code above is a sample method that does not return anything (void) and an operation to divide a and b. A try catch block will be able to catch any exception that may happen but throwing the exception that may happen is a different scenario. Inside the catch block, the C# compiler allows the block to be empty and not return anything. Most of the time this block can contain a logic to handle the exception such as logging and external error handlers. What happens if the developer leaves the block empty without the intention of leaving it empty? Errors in your application will not have any trace.

Some unit testing

[TestFixture]
public class SuppressedExceptions
{

[Test]
public void Division_With_Exception_Test() {

var silentException = new SilentExceptionSample();

Assert.Throws(() => silentException.DivideOperation(1,0));
}

[Test]
public void Division_With_NoException_Test()
{
var silentException = new SilentExceptionSample();

Assert.DoesNotThrow(() => silentException.DivideOperation(1,1));
}

}

I created a unit test to test the exception, on this sample above its the DivideByZeroException, that the method will throw when you divide a number by zero. The test fails in the first scenario since the error suppressed exception on the empty catch block.

Test Results

Ways to avoid

There can be ways to avoid this code smell. I suggest the following…

Unit Test

– Perform unit tests on exception handling on your code.

Code Snippet

– Use the code snippets on Visual Studio. If you want to use a try catch block, typing in “try” then tab will auto complete the whole block.

Resharper

– Tools such as Resharper can help in detecting code smells. See the warning below from the Resharper engine on the empty catch block.
Resharper Warning

This type of code smell can be the source of problem you are investigating now or something you can prevent while in development. You’ve been warned! :)

Snipping Tool Slow Response On New Snip

I recently encountered the snipping tool slow response on new snip on my Windows machine. It is causing which I call COD or “cause of delay” when I’m doing my work and trying to finish on time. What happens is when you capture a screen using the Snipping Tool, your machine just freezes up to 4 to 6 seconds before you can get the new snip and move around with your system. Yes, that is a hassle when all you want is to capture the screen. The Snipping Tool was a reliable tool in Windows until I encountered this issue.

Snipping Tool Slow Response Happens Only With Internet Explorer

I made a search in Google and found that this issue only happens when capturing the Internet Explorer screens. To try this I launched the Snipping Tool and made a new snip. I tried to capture the screen from my Chrome browser, it worked. I tried to capture the screen from my desktop and it worked as well. I went back to capture the screen from my Internet Explorer. Voila, my system instantly froze again!

How To Resolve The Issue on Snipping Tool Slow Response On New Snip

Under Tools, go to Options. The Snipping Tool Options will have an option for “Include URL below snips (HTML only)”. Uncheck that option and then try again to capture your Internet Explorer screen.

Snipping Tool Options

 

You will notice that capturing the screen on your Internet Explorer will now work with normal response and without freezing your system. Hope this helps. If you have other Snipping Tools tip, please share them in the comments section. Thanks! :)

How To Setup A Chrome Extension For Inline Installation

Chrome-Extension-Inline-Installation-Developer-Dashboard.png

In my previous blog post, I made a Chrome extension to make JSFiddle default to jQuery library.  I already uploaded the extension to the Chrome developer dashboard but did not press the "Publish" yet. I want to try and setup a chrome extension for … [Continue reading]

Chrome Extension To Make JSFiddle Default To jQuery Library

Developer dashboard category and pricing option

Google is now forcing users to download and install Chrome extensions that are from the Web Store only. In my previous post How To Make JSFiddle Default To jQuery Library – Update   I was able to create an extension in developer mode of Chrome but … [Continue reading]

How To Make JSFiddle Default To jQuery Library – Update

Disabled-User-Script-Not-From-Web-Store.PNG

In my resolve to not allow my self to go to the same problem of making JSFiddle default to jQuery library , I installed the user script from JohnKPaul to my Chrome browser as an extension. However, after restarting my machine and opening a Chrome … [Continue reading]

How To Make JSFiddle Default To jQuery Library

JSFiddle-Default-Library-No-JS.png

You click Run or hit Ctrl+Enter from your JSFiddle session but nothing happens. Yes, that simple jQuery script you are trying to run seems too simple not to work, isn't it? Then you notice that yes there is an option located in the left side of … [Continue reading]