是否总是将try-catch()写入方法内部?是否是静态void main()方法?是,静态void main()是方法吗?

时间:2019-05-27 07:26:54

标签: java serialization try-catch

为什么不在方法内部的try-catch起作用?

package ro.ase.acs.main;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import ro.ase.acs.classes.Addition;
import ro.ase.acs.classes.Robot;
import ro.ase.acs.interfaces.BinaryOperation;
import ro.ase.acs.interfaces.DisplayService;

public class Main {

    public static void main(String[] args) {
        Robot robot = new Robot();
        robot.setName("Lego Mindstorms");
        robot.setAutonomy(2);
        robot.setProductionYear(2015);

        try(FileOutputStream fos = new FileOutputStream("robot.bin");
            DataOutputStream data = new DataOutputStream(fos)) {//baga in fisier datele setate mai sus;

            data.writeUTF(robot.getName());
            data.writeFloat(robot.getAutonomy());
            data.writeInt(robot.getProductionYear());

        } catch(IOException e) {
            e.printStackTrace();
        }

        Robot robot2 = new Robot();
        try(FileInputStream fis = new FileInputStream("robot.bin");//se citesc datele din fisier;se creeaza un al 2-lea obiect cu datele pe care le-am bagat anterior in fisier;
            DataInputStream data = new DataInputStream(fis)) {

            robot2.setName(data.readUTF());
            robot2.setAutonomy(data.readFloat());
            robot2.setProductionYear(data.readInt());

        } catch(IOException e) {
            e.printStackTrace();
        }

        System.out.println(robot2);

        try(FileOutputStream fos = new FileOutputStream("robot.dat");
            ObjectOutputStream oos = new ObjectOutputStream(fos)) {//ia toate datele ,atributele gasite -n obiectul dat,anterior, cand le-am setat la inceputul programului
            //le scrie byte cu byte in fisier
                oos.writeObject(robot);
        } catch(IOException e) {
            e.printStackTrace();
        }

        Robot robot3 = new Robot();
        try(FileInputStream fis = new FileInputStream("robot.dat");//se baga datele, luate anterior, in robot3;
            ObjectInputStream ois = new ObjectInputStream(fis)) {
                robot3 = (Robot)ois.readObject();
        } catch(IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }


        System.out.println(robot3);
        System.out.println(new Robot());

        BinaryOperation op = new Addition();
        op = new BinaryOperation() {

            @Override
            public double operation(int x, int y) {
                return x - y;
            }
        };

        op = (int a, int b) -> a * b;
        op = (a, b) -> a / b;
        op = (a, b) -> { return Math.pow(a, b); };
        double result = op.operation(2, 3);

        System.out.println(result);

        DisplayService ds = (m) -> System.out.println(m);
        ds = m -> { System.out.print("Message: "); System.out.println(m);};
        ds.display("Hello from the Lambda expression");
    }

}

这是我老师写的代码,我读到try-catch()仅在方法内部有效,但在上面的代码中却不在方法内部有效,足以声明在try-catch之前创建一个新对象并编写try而不将其包含在方法中?

是的,静态void main()是一种方法,这就是为什么我们可以在静态void main()内编写try-catch语句而不会出现错误的原因。

1 个答案:

答案 0 :(得分:1)

public static void main(String[] args) { ... }是一种方法,try-catch块在此main方法内执行。

通常要回答您的问题:您可以在方法中编写try-catch块,也可以使用静态块(将在main方法之前执行):

class ClassName {
    static {
        //try-catch here
    }
}

但是,您不能写类似这样的内容:

class ClassName { 
    //try-catch here
}