Java: throwing exceptions

Is it possible to pass an object to the constructor when throwing exceptions?

Assume that the package contains the following code.

throw new MyException(object);

MyException Class:

Object object;
public MyException(Object object) {
    super();
    this.object = object;
}

public Object getObject() {
    return object;
}

And later, say, in the GUI, we must handle the exception that was sent to the service. We just get an exception and want to access the object passed in the constructor. It works. But is this true in terms of style?

Is using getObject () method correct?

+3
source share
4 answers

, , getObject. , . , . , , .

, , , ! , . - , .

, , . , . .

+2

, , , , .

getObject(), Object, - - , catch.

, . , "" - - . , . , .

0

, , , IMO .

, API , (. Exception API Doc), .

0

The class that catches the exception depends on the (static) class of the object that is encapsulated in the exception. It is generally recommended to minimize dependencies. Therefore, I will not encapsulate classes that are internal to the component into exceptions. I would encapsulate the public classes of a component into an exception only if the exception and the encapsulated class belong to the same component.

0
source

All Articles