Skip to content

Error Handling

RenΓ© Descartes Muala edited this page May 1, 2021 · 1 revision

Error Handling 🐞

Landb is a very boring library! Whenever you fail a variable name or landb_type, it stops your program and shows the error. This makes it safe, but sometimes we would like to override that configuration, by giving our alternative solution to problem.

Because of that, landb has different error types, they are: bit_name_error, anchor_name_error, empty_anchor_error, pull_error, overriding_bit_error.

The bit_name_error πŸ₯΄

This is a very common error, and it's caused when we try to get() or set([]) a variable by giving a wrong variable name, landb_type or index.

The anchor_name_error ?⛡️?

This error is caused when we try to set_anchor() in a variable.

Note: Anchors can only be set in contexts (Containers and Arrays).

The empty_anchor_error βˆ…

This error is caused when we try to use anchor without set_anchor() before.

The pull_error πŸ”—

This error is caused when we try to pull data from a corrupted LDS file.

The overriding_bit_error 🧱

This error is caused when we try without setting the override flag in set() method.

Caching errors πŸ”

Lets create a variable number and give it the value int(7)

    // Creating number
    database.set< int >("number", 7, lan::Int);

Now, what should happen if we try to get it but using the landb_type lan::Float?

    // Trying to get number with a wrong landb_type
    database.get< int >("number", lan::Float);

And something like this will be displayed:

libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: LANDB (bit_name_error): Unable to find bit "number".

And obviously it's because landb can't find the variable number of the landb_type lan::Float.

To catch that error we use a c++ try & catch block.

    try {
        instructions...
    } catch (error type) {
        alternative instructions...
    }

Implementing in our code

    try {
        // Trying to get number with a wrong landb_type
        database.get< int >("number", lan::Float);
    } catch (lan::errors::bit_name_error) {
        // Alterative solution
        std::cout << "Oops, number (float) doesn't exists" << std::endl;
    }

The print() method πŸ–¨

One of the most important methods is the print() method, it's used to debug your landb bit.

    // Usage
    database.print(context);
    // Example
    database.print();
    // Note: the default context is "main", so in most of the cases we don't need to specify it.

print() syntax πŸ“„

symbol meaning
| name type_index mem_address There is a variable called name of type specified by type_index and it's actually located at mem_address
(name): There is a container called name
[name]: There is an array called name

type indexes

index type
0 lan::Bool
1 lan::Int
2 lan::Long
3 lan::LongLong
4 lan::Float
5 lan::Double
6 lan::Char
7 lan::String
8 lan::Unsafe

Example:

[ classes ]:
	[  ]:
		(  ):
			| name 7 0x100608310
			| average 4 0x100608330
			| has_passed 0 0x100606c20
		(  ):
			| name 7 0x1006095e0
			| average 4 0x1006a6200
			| has_passed 0 0x1006a5dc0
		(  ):
			| name 7 0x1006a5ca0
			| average 4 0x1006098c0
			| has_passed 0 0x1006a3e90

Meaning:

  • There is an array classes
  • classes[0] witch is also an array, has three containers
  • Each container has name 7-> (string), average 4 -> (float) and has_passed 0->(bool)

Note: array elements doesn't have a name.

Back to our code lets add

database.print();

Then

    
    return 0;
}

Here is the entire code {✨}

#include <iostream>
#include "landb.hpp"

int main(int argc, const char * argv[]) {
    
    // Database
    lan::db database;
    
    // Creating number
    database.set<int>("number", 7, lan::Int);
    
    try {
        // Trying to get number with a wrong landb_type
        database.get< int >("number", lan::Float);
    } catch (lan::errors::bit_name_error) {
        // Alterative solution
        std::cout << "Oops, number (float) doesn't exists" << std::endl;
    }
    
    database.print();
    
    return 0;
}

Go back to Home.