What about using multiple dispatch? handle(::ExceptionType1, ::MyObject) = ... handle(::ExceptionType2, ::MyObject) = ... handle(::ExceptionType3, ::MyObject) = ... handle(e::Exception, ::MyObject) = rethrow(e) try # Access a dict here catch e handle(e, dict) end
Am Montag, 17. November 2014 15:49:24 UTC+1 schrieb Luthaf: > > Hello ! > > Is there a way to catch an exception by type in Julia ? Coming from > python, I am very tempted to do this kind of things: > ``` > try > # Access a dict here > catch e > if isa(KeyError, e) > # Handle the KeyError, as I know what to do in that case > else > # Re-throw to upper level > throw(e) > end > ``` > But that is a lot of boilerplate when used many times. > > Maybe it is not the Julia way to express this kind of problem (handling > one type of exception, while letting the others go up), but I could not > find something about it. > I can not just remove all the exceptions as some of them are thrown by > Base. > > > Thank for yours answers ! > Guillaume >