# Exception handling

**URL:** https://discourse.julialang.org/t/exception-handling/3853
**Category:** New to Julia
**Created:** [May 22, 2017, 2:23pm UTC](https://discourse.julialang.org/t/exception-handling/3853 "2017-05-22T14:23:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)
#### Post date: [May 22, 2017, 2:23pm UTC](https://discourse.julialang.org/t/exception-handling/3853/1 "2017-05-22T14:23:32Z")

</div>

What is a recommended way to handle exception/errors in larger projects?  
Is there similar mechanism as RAII in C++ (can I be sure that a resource is cleaned if an exception is thrown; or have I always use `finally` and handle resource)?

Assume an exception with some additional info, see C++ example below, what is a best way in Julia…

```julia
class MyException : public std::exception
{
public:
  explicit MyException(const char* msg) :MyException(0, msg) {}
  explicit MyException(int code, const char* msg) : _code{ code }, std::exception{ msg } {}
  int code() { return _code; }

protected:
  int _code;
};

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [May 22, 2017, 4:44pm UTC](https://discourse.julialang.org/t/exception-handling/3853/2 "2017-05-22T16:44:19Z")

</div>

The [`do` block syntax](https://docs.julialang.org/en/stable/manual/functions/#do-block-syntax-for-function-arguments) is often used as a way to create context managers or give RAII-like behavior. For example, Julia provides this definition for you:

```julia
function open(f::Function, args...)
    io = open(args...)
    try
        f(io)
    finally
        close(io)
    end
end

```

which allows a user to write code like:

```julia
open("foo.txt") do f
  write_some_data_into_a_file(f)
end

```

and ensures that the file is properly closed no matter what the user’s code does. You could implement the same pattern for the resources in your code.

---

<div class="post-metadata">

### Author: ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)
#### Post date: [May 23, 2017, 7:43am UTC](https://discourse.julialang.org/t/exception-handling/3853/3 "2017-05-23T07:43:11Z")

</div>

Thanks, it seems that RAII-like behavior is not a built in feature in Julia.

I’m trying to find a general way how to handle errors in an a larger project.

The do block syntax is nice to use once, but it seems, it has to be always wrapped in the do block. Assume a simple C++ code: (I know the design of opening a file and do something in between is not a correct way, but such a code is common in everyday life) A comparison of C++ (assuming C++ 11 or higher) and Julia follows:

```julia
void foo()
{
  std::ifstream f;
  f.open("foo.txt");
  // do something or call something that throws and write results....
} // whatever happens the resource is closed

```

in Julia I have to do following to achieve the same safety:

```julia
import Base:open
function open(f::Function, args...)
    io = open(args...)
    try
        print_with_color(:green, "writing...\n")
        f(io)
    finally
        print_with_color(:blue, "closing.\n")
        close(io)
    end
end

function foo()
  open("foo.txt") do f
     # do something, call something that throws and write results....
     # I have to be sure to write into the file here
  end # the file is closed here I cannot use it any more
  
end

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [May 23, 2017, 8:55pm UTC](https://discourse.julialang.org/t/exception-handling/3853/4 "2017-05-23T20:55:51Z")

</div>

One additional option that’s been suggested here before is [Defer.jl](https://github.com/adambrewster/Defer.jl) which provides some helpful macros for scoping and resource cleanup.

---

<div class="post-metadata">

### Author: ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)
#### Post date: [May 24, 2017, 7:13am UTC](https://discourse.julialang.org/t/exception-handling/3853/5 "2017-05-24T07:13:20Z")

</div>

Thanks for hint, I’m gonna check the Defer.jl in more details. Though, the intro confirmed, that the resource handling is one of the key features for development.

---

<div class="post-metadata">

### Author: ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)
#### Post date: [May 25, 2017, 10:53am UTC](https://discourse.julialang.org/t/exception-handling/3853/6 "2017-05-25T10:53:36Z")

</div>

Here is proposed error handling for larger project.

We define custom exception, used for our code…

```julia
type MyException <: Exception
    code::Int64
    msg::String
    
    function MyException(code::Int64, msg::String)
        new(code, msg)
    end
end

```

`fnThrow` is our main function where we call a lot of code in `try catch` block

```julia
function fnThrow(fn::Function)
    try
        fn()
    catch exc
        if(isa(exc, MyException))
            print_with_color(:green, "$exc\n")
        elseif(isa(exc, Exception))
            print_with_color(:yellow, "$exc\n")
        else
            print_with_color(:red, "! $exc\n")
        end
    end
end

```

and testing the error handling

```julia
fn1() = sqrt(-1)
fn2() = throw(MyException(11, "throwing"))
fn3() = throw(Exception("exception"))
fn4() = error("error")
fn5() = throw("string")
fn6() = throw(1)

fnThrow(fn1)
fnThrow(fn2)
fnThrow(fn3)
fnThrow(fn4)
fnThrow(fn5)
fnThrow(fn6)

```
