在我的代码库中,我有一些重试功能,它将异常存储在变量中,最后在变量中抛出异常。想象一下这样的事情
Exception exc = null;
while(condition){
try {
// stuff
} catch (Exception e){
exc = e;
}
}
if (e != null){
throw e;
}
Run Code Online (Sandbox Code Playgroud)
现在,由于这是一条throw e语句而不是一条throw语句,因此原始堆栈跟踪丢失了。有没有办法进行重新抛出以保留原始堆栈跟踪,或者我需要重组我的代码以便我可以使用语句throw?
我希望能够将实例变量的使用限制为一种方法,并且其他用法不应该是可能的(编译错误或警告)。例如
public class Example
{
public string Accessor()
{
if (SuperPrivate == null) // allowed
{
SuperPrivate = "test"; // allowed
}
return SuperPrivate; // allowed
}
private string SuperPrivate;
public void NotAllowed()
{
var b = SuperPrivate.Length; // access not allowed
SuperPrivate = "wrong"; // modification not allowed
}
public void Allowed()
{
var b = Accessor().Length; // allowed
// no setter necessary in this usecase
}
}
Run Code Online (Sandbox Code Playgroud)
无法使用惰性、自动属性或封装在单独的对象中。我考虑过扩展 ObsoleteAttribute,但它是密封的。
我有一段很长的代码,看起来像这样
Kwas a1 = new Kwas("Kwas Azotowy(V)", "HNO3");
// etc..
Kwas a17 = new Kwas("Kwas FluorkoWodorowy", "HF");
Run Code Online (Sandbox Code Playgroud)
如何将其写为数组?我试过类似的东西
Kwas[] a = new Kwas[17]
Run Code Online (Sandbox Code Playgroud)
但它没有用.
我的"Kwas"课程如下所示:
public class Kwas {
String name;
String formula;
public Kwas( String nazwa, String wzor)
{
name = nazwa;
formula = wzor;
}
void setName(String c)
{
name = c;
}
void setFormula(String c)
{
formula = c;
}
public String getName()
{
return name;
}
public String getFormula() {return formula;}
Run Code Online (Sandbox Code Playgroud)
}