Monday, 14 March 2016

Julia: Built in exceptions

All the exceptions derived from type Exception. Following table summarizes all the built in exceptions.

Exception
Description
ArgumentError
When you call a function with invalid arguments, you will get this error.

julia> float("12we")
ERROR: ArgumentError: invalid number format "12we" for Float64
 in parse at parse.jl:164
 in float at parse.jl:167
BoundsError
When you try to access an element of array at invalid index, you will get this error.

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
DivideError
Integer division was attempted with a denominator value of 0.
DomainError
When you call a function with arguments that are outside of the domain, this error will occur.

For example, the domain of the square root function is the set of positive real numbers. Therefore, a domain_error exception is to be thrown when the arguments of a function are not contained in its domain.

julia> sqrt(-1)
ERROR: DomainError:
sqrt will only return a complex result if called with a complex argument. Try sqrt(complex(x)).
 in sqrt at math.jl:146

julia> 2^-2
ERROR: DomainError:
Cannot raise an integer x to a negative power -n.
Make x a float by adding a zero decimal (e.g. 2.0^-n instead of 2^-n), or write 1/x^n, float(x)^-n, or (x//1)^-n.
 in power_by_squaring at /Applications/Julia-0.4.1.app/Contents/Resources/julia/lib/julia/sys.dylib
 in ^ at /Applications/Julia-0.4.1.app/Contents/Resources/julia/lib/julia/sys.dylib
EOFError
This error occurs, when there is no more data available to read from a file or stream.


ErrorException(msg)
Generic error type. The error message, in the .msg field, may provide more specific details.
InexactError
This error occurs when you try to convert a value ‘a’ to a type ‘T’ that can’t represent the value ‘a’.

julia> convert(Int, 10.0)
10

julia> convert(Int, 10.01)
ERROR: InexactError()
 in convert at int.jl:209
InitError(mod::Symbol, error)
This error thrown when you try to load a module (or) while calling __init__ function.
InterruptException
Thrown, when a process is interrupted by CTRL+C.

julia> function process_data(seconds::Int64)
           sleep(seconds)
       end
process_data (generic function with 1 method)

julia> process_data(20)
^CERROR: InterruptException:
KeyError
Thrown when you try to access non-existent  element from a dictionary or set.

julia> d1=Dict(1=>1, 2=>4, 3=>9)
Dict{Int64,Int64} with 3 entries:
  2 => 4
  3 => 9
  1 => 1

julia> d1[1]
1

julia> d1[5]
ERROR: KeyError: 5 not found
 in getindex at dict.jl:718
LoadError(file::AbstractString, line::Int, error)
An error occurred while includeing, requireing, or using a file.
OutOfMemoryError
This error thrown, when system unable to allocate memory for processing.
ReadOnlyMemoryError
Throws when you try to write to memory that is read-only.
MethodError
Thrown when a method with given signature is not exist.

julia> function process_info()
       end
process_info (generic function with 1 method)

julia> process_info(1)
ERROR: MethodError: `process_info` has no method matching process_info(::Int64)
OverflowError
Thrown when the result of the expression is not fit to a type T.

julia> a=factorial(10000)
ERROR: OverflowError()
 in factorial_lookup at combinatorics.jl:29
 in factorial at combinatorics.jl:37
ParseError
The expression passed to the parse function could not be interpreted as a valid Julia expression.
SystemError
Thrown when a system call failed.
TypeError
A type assertion failure, or calling an intrinsic function with an incorrect argument type.
UndefRefError
The item or field is not defined for the given object.

julia> xx
ERROR: UndefVarError: xx not defined
UndefVarError
A symbol in the current scope is not defined.

julia> function print_data()
           println(x)
       end
print_data (generic function with 1 method)

julia> print_data()
ERROR: UndefVarError: x not defined
 in print_data at none:2






Previous                                                 Next                                                 Home

No comments:

Post a Comment