Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - ERROR HANDLING


Just imagine, you are asked to enter your gmail Id and Password to login to your Gmail account. But, by mistake you have entered a wrong password. And what if, Gmail exits without giving you a second chance to enter the Password? - It is not acceptable.


Same thing applies for any application. We are allowed to make some sort of mistakes. So that we get a second chance to rectify that. Such mistakes in Go are called as Errors.


So, the Errors should be handled/caught at runtime, and also the entire application should not close due to that Error.


It is same as your Gmail account doesn't close when someone types a wrong password and at the same time Gmail needs to keep a track on how many times the user is entering a wrong Password.


Say for example, you have written a program to divide two numbers then add them. But by mistake, you tried to divide the number by zero.


Let us see what happens in the below example.


Example :



package main
import "fmt"
     
func main() { 
     
    
    z := 4 / 0
    fmt.Println("The divided result is ",z)
            
    k := 5 + 2
    fmt.Print("The added result is ",k)
}


Output :



 division by zero

So, if you see the above example, we have tried to divide the number '4' by '0' and initialise it to variable 'z'.


z := 4 / 0

And since, we tried to divide '4' by '0'. It ended up with an error.


division by zero

That could be ok. But the problem is, the addition operation was not performed for the above error.


k := 5 + 2
print("The added result is ",k)

And the program got halted. Which is not acceptable.


And Go provides a solution to the above problem, with something called as a 'defer', 'panic' and 'recover' mechanism.


What Go does is, it handles errors in a Function. And returns the errors as a variable as return value from a Function. If there are no errors then the error variable would be 'nil'.



Handling Error in a Function


So, you know that there can be a divide by zero error.


And all you need to do is, place the division operation inside a Function. So, if there is any error, the Function would return the error object and the program execution won't be halted.


Example :



package main
import "fmt"
import "errors"
    
func division(firstNum int, secondNum int) (int, error) {
        
    if (secondNum == 0) {
        return -1, errors.New("Divide by zero not allowed")
    } else {	
        result := firstNum / secondNum
        return result, nil
    }
}
     
func main() { 
     
    x := 4
    y := 0
    z, err := division(x, y)
            
    if (err != nil) {
        fmt.Println(err)
    } else {	
        fmt.Println("The divided result is ",z)
    }	
            
    k := x + y
    fmt.Println("The added result is ",k)
}


Output :



 Divide by zero not allowed
 The added result is 4

Now, if you see the above output, although we got the above message 'Divide by zero not allowed'.


But the program didn't crash. And printed the added result.


Now, let us see how it worked.


So, in the above code, we have created a Function called 'division()' and put the division operation inside the Function.


func division(firstNum int, secondNum int) (int, error) {
	
    if (secondNum == 0) {
        return -1, errors.New("Divide by zero not allowed")
    } else {	
        result := firstNum / secondNum
        return result, nil
    }
}

And the 'division()' Function is where the actual error is handled. And it's quite simple.


You just check, if the second number is zero, then there should be a 'divide by zero' error.


if (secondNum == 0) {
    return -1, errors.New("Divide by zero not allowed")
}

And you handle the error using the 'New()' Function of 'errors' package.


return -1, errors.New("Divide by zero not allowed")

And what happens is, '-1' gets assigned to 'z'(Because the number couldn't be divided) and 'err' is assigned with "Divide by zero not allowed".


java_Collections

And since the 'err' variable is not 'nil'.


if (err != nil) {
    fmt.Println(err)
}

The below output from 'err' variable is printed.


Divide by zero not allowed

And most importantly, the addition operation didn't get halted. And the below line executes,adding two numbers.


k = x + y
print("The added result is ",k)