你可以在构造函数中使用throw或try-catch吗?
如果是这样,拥有一个可以引发异常的参数的构造函数的目的是什么?
这个构造函数是一个例子:
public Chat()
{
chatClient = new Client(Configuration.LoginEmail, Configuration.LoginPassword);
chatRoom = chatClient.JoinRoom(Configuration.RoomUrl);
}
Run Code Online (Sandbox Code Playgroud)
该行chatRoom = chatClient.JoinRoom(Configuration.RoomUrl);可以抛出异常.
我有这个构造函数抛出异常
GenericSocket::GenericSocket(const string& hostname,
const string& servname):
_hostname(hostname),
_servname(servname)
{
initHints();
int rv;
if((rv = getaddrinfo(_hostname.c_str(),
_servname.c_str(),
&_hints,
&_servinfo)) != 0) {
throw GenericSocketException();
}
}
Run Code Online (Sandbox Code Playgroud)
initHints()执行_hints的memset并设置一些变量.
我用google测试框架测试它,如下所示:
TEST(CreateObject2, getaddrinfoException)
{
mgs_addrinfo_return = 1;
ASSERT_THROW(new GenericSocket("testhost", "4242"), GenericSocketException);
}
Run Code Online (Sandbox Code Playgroud)
测试因核心转储失败:
[ RUN ] CreateObject2.getaddrinfoException
socket creation failed
terminate called after throwing an instance of 'common::GenericSocketException'
what(): Socket creation failed
[1] 43360 abort (core dumped) ./bin/test_common
Run Code Online (Sandbox Code Playgroud)
除了我不知道到底出了什么问题的事实,我怀疑一些未初始化的对象被删除(?),很多似乎发生在幕后,所以我开始怀疑在构造函数中抛出异常是否是好习惯.是否可以更好地将此功能放在另一个我可以在创建对象后调用的函数中,然后处理异常?
如果一个类在对象创建期间需要多个字段信息,并且它允许更少的信息.我们有两个选项
1.提供多个构造函数,或
2.允许客户端在创建对象时传递null参数.
其中最佳做法就是这些.
例:案例1:
public class Test {
Test(A ob1,B ob2, C ob3){
}
Test(A ob1,B ob2){
this(ob1, ob2, null);
}
public static void main(String args[]){
Test ob = new Test(new A(),new B());
}
}
Run Code Online (Sandbox Code Playgroud)
案例2:
public class Test {
Test(A ob1,B ob2, C ob3){
}
public static void main(String args[]){
Test ob = new Test(new A(),new B(), null);
}
}
Run Code Online (Sandbox Code Playgroud)
我在同一个类中使用了main方法.请在其他课程中考虑这些主要方法.
谢谢,
Shantanu
我想问一下,在ES6中如何才能在没有setter(readOnly)属性的情况下使用getter?为什么Webstorm告诉我这是一个错误?
这是我的代码:
class BasePunchStarter {
constructor(id,name,manufacturer,description,genres,targetPrice) {
if (new.target==BasePunchStarter) {
throw new TypeError("BasePunchStarter class cannot be instantiated directly!");
}
if (typeof id =="number") {
// noinspection JSUnresolvedVariable
this.id = id;
} else throw new TypeError("ID must be a number!");
if (typeof name=="string") {
// noinspection JSUnresolvedVariable
this.name = name;
} else throw new TypeError("Name must be a string!");
if(typeof manufacturer=="string") {
// noinspection JSUnresolvedVariable
this.manufacturer = manufacturer;
} else throw new TypeError("Manufacturer must be a string!");
if (typeof description=="string") { …Run Code Online (Sandbox Code Playgroud) 这是情况.假设我有一个名为MYFoo的课程.这是初始化程序:
-init
{
self = [super init];
if (self)
{
// during initialization, something goes wrong and an exception is raised
[NSException raise ...];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
现在我想在其他地方使用MYFoo对象,所以我使用了一个常见的模式:
MYFoo *foo = [[[MYFoo alloc] init] autorelease];
Run Code Online (Sandbox Code Playgroud)
但是会发生什么,即使第二部分有一个try/catch,一个MYFoo对象将被分配,异常将被抛出,自动释放错过,未初始化的MYFoo对象将泄漏.
这里应该发生什么来防止这种泄漏?
constructor ×3
c# ×1
c++ ×1
ecmascript-6 ×1
es6-class ×1
getter ×1
googletest ×1
java ×1
javascript ×1
memory-leaks ×1
objective-c ×1