Error
Example 1
package main
import "fmt"
import "errors"
func Sum(a int, b int) (int, error) {
c := a + b
if c == 0 {
return c, errors.New("Result is Zero")
}
return c, nil
}
func main() {
sum, error := Sum(0, 0)
if error != nil {
fmt.Println("Error Msg : ", error)
}
fmt.Println("Sum:", sum)
}
<?php
function Sum($a,$b){
$a=(int)$a;
$b=(int)$b;
$c=$a+$b;
if($c==0){
return array($c,"Result is Zero");
}
return array($c,false);
}
$data=Sum(0,0);
$sum=$data[0];
$error=$data[1];
if($error!==false){
echo "Error Msg : ".$error;
}
echo "\n";
echo $sum;
Example 2
package main
import "fmt"
func Sum(a int, b int) int {
c := a + b
if c == 0 {
panic("Result is Zero")
}
return c
}
func main() {
sum := Sum(0, 0)
fmt.Println(sum)
}
<?php
function Sum($a,$b){
$a=(int)$a;
$b=(int)$b;
$c=$a+$b;
if($c==0){
throw new Exception("Result is Zero");
}
return $c;
}
$sum=Sum(0,1);
echo $sum;
Example 3
package main
import "fmt"
func Sum(a int, b int) int {
c := a + b;
if c == 0 {
panic("Result is Zero")
}
return c
}
func main() {
defer func() {
// Panic Varmı kardeş?
if r := recover(); r != nil {
fmt.Println(r)
}
}()
sum := Sum(0, 0)
fmt.Println(sum)
}
<?php
function Sum($a,$b){
$a=(int)$a;
$b=(int)$b;
$c=$a+$b;
if($c==0){
throw new Exception("Result is Zero");
}
return $c;
}
try{
$sum=Sum(0,0);
}catch(Exception $e){
echo $e->getMessage();
}
echo $sum;