GO lang exception handling

huangapple go评论81阅读模式
英文:

GO lang exception handling

问题

我是你的中文翻译助手,以下是你提供的代码的翻译:

我对Go语言还不熟悉。如何在Go中实现子类型继承并处理异常?我尝试了一些方法,但是无论如何都无法使其正常工作。

package main

import (
	"fmt"
)

type class1 struct{}

func (c *class1) m1() error {
	fmt.Println("m1 in class1")
	return nil
}

type class2 struct {
	class1
}

func (c *class2) m1() error {
	fmt.Println("m1 in class2")
	return nil
}

func main() {
	obj := &class1{}

	err := obj.m1()
	if err != nil {
		fmt.Println("ioexception")
	}
}

请注意,Go语言中没有直接的异常处理机制,而是使用错误返回值来处理异常情况。在上述代码中,我们定义了class1class2两个结构体,并在class2中重写了m1方法。在main函数中,我们创建了class1的实例obj,并调用了m1方法。如果m1方法返回了错误,我们将打印"ioexception"。

英文:

I am new to go lang. How to achieve subtype inheritance in GO and handle exceptions in it? I am trying something to this but somehow I am not able to get it to work.

import java.io.*;
import java.rmi.*;

class class1
{
    public void m1() throws RemoteException
    {
        System.out.println("m1 in class1");
    }
}

class class2 extends class1
{
    public void m1() throws IOException
    {
        System.out.println("m1 in class2");
    }
}

class ExceptionTest2
{
    public static void main(String args[])
    {
        class1 obj = new class1();
        
        try{
            obj.m1();
        }
        catch(RemoteException e){
            System.out.println("ioexception");
        }
    }
}

答案1

得分: 7

正如其他人已经指出的那样,Go语言与Java非常不同。这意味着你不会有与Java代码“非常相似”的东西。

嵌入而不是继承

Go语言没有你可能熟悉的"继承"。你可能会找到最接近的是所谓的嵌入

虽然一个嵌入的方法可能会被父类的方法遮蔽并充当一种覆盖,但这不是在Go语言中解决编程任务的常见方式。

使用错误而不是异常

Panic不应该被用作异常。如果你想编写Go代码,应该返回错误来通知调用的函数/方法发生了错误。

在Go语言中,你的代码可能是这样的:

package main

import (
	"errors"
	"fmt"
)

var (
	RemoteError = errors.New("远程错误发生")
	IOError     = errors.New("IO错误发生")
)

type Struct1 struct{}

func (s *Struct1) m1() error {
	fmt.Println("Struct1中的m1")
	return nil // 或者返回RemoteError
}

type Struct2 struct {
	Struct1
}

func (s *Struct2) m1() error {
	fmt.Println("Struct2中的m1")
	return nil // 或者返回IOError
}

func main() {
	s1 := &Struct1{}

	err := s1.m1()
	if err != nil {
		fmt.Println(err.Error())
	}
}

输出:

Struct1中的m1

Playground: http://play.golang.org/p/VrhvtQDXCx

英文:

As people already has pointed out, Go is very different from Java.
That means you will not have something "very much similar" to the Java code.

Embedding instead of inheritance

Go does not have inheritance as you might be familiar with it. The closest you might find is called embedding.

And while an embedded method might be shadowed by a method of the parent and act as a sort of override, this is not a common way to solve your programming tasks in Go.

Errors instead of Exceptions

Panics are not to be used as exceptions. If you want to write Go code, you return errors to inform the calling function/method that something went wrong.

What your code might look like in Go:

package main

import (
	"errors"
	"fmt"
)

var (
	RemoteError = errors.New("A remote error occured")
	IOError     = errors.New("An IO error occured")
)

type Struct1 struct{}

func (s *Struct1) m1() error {
	fmt.Println("m1 in Struct1")
	return nil // or RemoteError
}

type Struct2 struct {
	Struct1
}

func (s *Struct2) m1() error {
	fmt.Println("m1 in Struct2")
	return nil // or IOError 
}

func main() {
	s1 := &Struct1{}

	err := s1.m1()
	if err != nil {
		fmt.Println(err.Error())
	}
}

Output:

m1 in Struct1

Playground: http://play.golang.org/p/VrhvtQDXCx

huangapple
  • 本文由 发表于 2014年4月25日 13:18:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/23284699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定