Sunday 13 March 2016

Julia: Exceptions


Exception is an event that disrupts the normal flow of execution. The exception contains information about exception type, the line where the exception occurred and some useful information.
julia> arr=[10, 20]
2-element Array{Int64,1}:
 10
 20

julia> arr[0]
ERROR: BoundsError: attempt to access 2-element Array{Int64,1}:
 10
 20
  at index [0]
 in getindex at array.jl:282

In above snippet, I tried to access element of array at index 0, so I got BoundsError.

Handling exceptions
Julia provides ‘try-catch-finally’ construct to handle exceptions. Whenever you felt that, there is a possibility of occurring an exception in the code, then place the entire code in the try block. Provide handlers to handle the exception.

After a method throws an exception, the run time system tries to find exception handlers to handle it.

If the Run time system finds a handler to handle the exception, then it executes the block of code in the handler, and Proceed execution from the catch block onwards. Other wise program simply terminates.

Syntax
try
         statements
catch ex
         statements
finally
         statements
end

Example
julia> try
           arr=[10, 20]
           arr[0]=20
           println("I am not going to execute")
       catch ex
           println("Caught excepion")
           showerror(STDOUT, ex)
       finally
           println("\nCompulsory execution block")
       end
Caught excepion
BoundsError: attempt to access 2-element Array{Int64,1}:
 10
 20
  at index [0]
Compulsory execution block


Handling different types of exceptions
Use following template to handle different types of exceptions.

julia> function info_process(a)
           try
               if(a==1)
                   arr=[10, 20]
                   arr[0]=100
               else
                   sqrt(-1)
               end
           catch ex
               if isa(ex, DomainError)
                   println("DomainError occured")
                   showerror(STDOUT, ex)
               elseif isa(ex, BoundsError)
                   println("BoundError occured")
                   showerror(STDOUT, ex)
               else
                   showerror(STDOUT, ex)
               end
           finally
               println("Compulsory execution")
           end
       end
info_process (generic function with 1 method)

julia> info_process(1)
BoundError occured
BoundsError: attempt to access 2-element Array{Int64,1}:
 10
 20
  at index [0]Compulsory execution

julia> info_process(2)
DomainError occured
DomainError()Compulsory execution


Finally block
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. Finally block mainly used to perform clean up operation like closing file streams, flushing the data etc.,

Note
You can use any combination try-catch, try-finally, try-catch-finally.



Previous                                                 Next                                                 Home

No comments:

Post a Comment