HadesLang Doc
  • Overview
  • Getting Started
    • Installing Hades
    • Basic Syntax
    • Coding Conventions
  • Language Spec
    • Foundation
      • Types
      • Built-in functions
    • Operators
      • Comparison and equality
      • Logical operators
      • Bitwise operators
      • Compound Assignment Operators
      • Operator overloading
    • Control flow
      • Conditions
      • Loops
      • Exception handling
      • Ternary and nullcheck operators
      • Pipelines
    • Classes and variables
      • Declaring variables
      • Declaring classes
      • Declaring structs
      • Declaring protos
      • Declaring arrays
      • Type conversions in simple variable types
    • Actors
      • Message passing
      • GenServer
      • Channels
    • Functions and lambdas
      • Declaring functions
      • Declaring lambdas
    • Functions of simple types
      • int
      • string
      • float
      • bool
      • atom
      • pid
    • Other
      • Annotations
      • Comments
      • Preprocessor statements
      • Reflection
      • Script file arguments
      • this keyword
  • Core Libraries
    • Standard library
      • std:io
        • console
        • file
        • directory
      • std:exceptions
      • std:int
      • std:internals
        • annotations
          • findMethod
          • findMethodInProto
          • findMethods
          • findMethodsInProto
        • function
      • std:collections
        • map
        • list
      • std:math
        • math
        • constants
        • matrix
      • std:networking
      • std:os
      • std:params
      • std:string
      • std:sql
    • Extra libraries
      • mssql:client
  • Other
    • Tools
    • Examples
    • Todos
Powered by GitBook
On this page
  • try-catch-else block
  • Example
  • Example
  • raise statement
  • Example
  1. Language Spec
  2. Control flow

Exception handling

Exceptions in Hades

In Hades, everything can be an exception. There is a collection of standard exceptions (in std:exceptions) but nothing prevents you from throwing any object as an exception. This comes in quite handy when you want to handle multiple exceptions with very different error sources.

try-catch-else block

If you want to catch all exceptions, don't specify a type in the catch block.

Example

with client from mssql:client
with server from std:http
with console from std:io
with file from std:io

var connection object = client("Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password")
    
try
    connection.open()
    console.out("Connection opened!")
    connection.close()
catch(object::SqlException e) //here, an SqlException is caught
    console.out("SqlException was caught!")
catch(e) //in the case that any other exception was raised, this block is invoked
    console.out("An unknown exception was caught!")
end

try
    file.read("1.txt")
catch(e)
    //ignored
end

try
    var f object::File = file("2.txt")
catch(object::FileNotFoundException e)
    console.out("File {} not found!".format(e.file))
end

let srv = server(port=8080)
srv.get("/:path", {req, res => 
    let path = req.param["path"]
    try
        if (file.exists(path))
            let f = file.open(path)
            res.send(f.read())
        else
            raise 404
        end
    catch(int status)
        res.status(status)
    else
        res.status(200)
    end
})

srv.start()

Code in an else block after a try-catch will be executed if the execution didn't fail.

Example

with console from std:io
var number

try
    number = int(console.in())
catch(e)
    console.out("Could not parse number")
else
    console.out("Number is {}".format(number))
end

raise statement

The raise statement raises an exception.

Example

with exceptions from std:exceptions

func equals(a object, b object)
    if(a == null)
        raise exceptions.ArgumentNullException("{} is null".format(nameof(a)))
    else if(b equals null)
        raise exceptions.ArgumentNullException("{} is null".format(nameof(b))
    end
    
    put a == b
end

PreviousLoopsNextTernary and nullcheck operators

Last updated 5 years ago