Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Handable: A Cool Way to Treat Your Exceptions Within Code Flow

DZone's Guide to

Handable: A Cool Way to Treat Your Exceptions Within Code Flow

What about a cool way to nicely prevent undesired exceptions blow up your flow without using the ancient try/catch? That's what I've done in Handable! I bet you can Handle it.

Free Resource

What about a cool way to nicely prevent undesired exceptions blow up your flow without using the ancient try/catch? That's what I've done in Handable! I bet you can Handle it.

People from my current work asked me to make this application, which can never stop or break. My main problem to accomplish the requested functionality is that the application's main purpose is to import data in a batch, from WebServices to a local base, from time to time, so other applications may use the information. This WebServices does not send me reliable information, so at any time, they can send me a char where rather it had been expected an integer and so on. If any situation like this example happens, I could not do anything. I would only need to proceed with the flow and log that parse-like exceptions would not help me at all, it would only consume my precious time in my multi-threaded application. Besides that empty catch blocks are as ugly as use goto.

I will demonstrate one sample of Handable, I've implemented in C#, but I will make the Java version as soon as possible. Functional languages like Scala and Clojure does not need this mechanism, they already have their cool Monad ways to treat their exceptions in a chain.

So a simple, but yet, astonish sample :

new Handable() // first of all, lets instantiate, shall we?
        .ForValue("212") // you can add as many arguments as you want
        .ForFunction<int,string>(Convert.ToInt32) // the actual function to be called and handled
        .ForFunction<int,int>((a,b)=>a+b) // you can chain as many functions as you like
        .OnError(() =>
        {
            Console.WriteLine("That is not an integer !!");
        }) // we`ve just added a simple function to be called if any exception occur
       .OnError((ex)=>
        {
            Console.WriteLine("Lets print this stink error !!" + ex.Message);
        }) // another function but that one recieves the exception as argument
       .OnSucess(()=>
        {
            Console.WriteLine("We`ve just parsed an integer and then added another to that");
        })// now a function to be called if all goes well
        .Execute<int,string>(); // now we execute the first function added (parse)
        .Execute<int,int>(4); // notice that this function takes an integer as an argument,
        //because the original add take to integers, the first one is that we`ve parsed
        //we have to call the functions in the order we`ve added they like a FIFO
        .Finish<int>(0); // if any errors occurs it will simply return zero

So, that's it! Easy no?? You just go chaining everything, you can use as many functions as you wish, as many OnErros and so on. It will execute the functions transparently. 

The returns of the methods are divided into two interfaces to prevent programming errors like if you had already called Execute and then invoked ForValue again.

The only part I've hated is the way C# treats functions, we are obligated to declare in the Func built in type the parameters and the returns of each function, since C# is static typed. That is why we have to use these horrible generics in each ForFunction and Execute calls.

The Handable code, it's on my GitHub -> 

https://github.com/MDIB/SimpleErrorHandler/

If you have any cool ideas or want to implement in another language, please contribute!!

Thanks a lot guys.

Topics:
c# ,exception handler ,java ,lambda

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}