我正在尝试编写一些捕获特定异常的代码,并为调用堆栈处理更高级别的内容抛出更有用的代码,同时捕获更多常规异常并处理它们.
代码是这样的:
try
{
// Do stuff
}
catch (SomeException e)
{
throw new SomeExceptionWithContextInfo();
}
catch (Exception e)
{
// Handle unexpected exception gracefully
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是一般的例外是捕获我的新异常.有没有办法避免这种情况?
我当前的解决方案涉及检查异常的类型,如果它的类型是我刚刚创建的类型,则再次抛出它.
我写了一个返回字符串格式化日期的毫秒值的方法,由于某种原因,它给了我将来39000年的日期.任何想法为什么?
private long getTimeInMs(String currentStartTimeString) {
//String newDateString = currentStartTimeString.substring(6,10)+"-"+currentStartTimeString.substring(3, 5)+"-"+currentStartTimeString.substring(0, 2)+ "T" + currentStartTimeString.substring(11);
String newDateString = currentStartTimeString.substring(0,19);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long timeInMs;
try {
timeInMs = df.parse(newDateString).getTime();
} catch (ParseException e) {
log.error("Failed to parse current Start Time",e);
return 0;
}
return timeInMs;
}
Run Code Online (Sandbox Code Playgroud)
如果我输入日期字符串"2009-07-07 10:51:01.15",则返回1246960261000,实际上是星期三06月06日41484 11:16:40 GMT + 0100(GMT日光时间)
好吧,我认为问题在于它是否已经超越了Java epoc并且我正在针对unix时代进行评估......
我看到了大量带有以下语法的代码片段
using (RandomType variable = new RandomType(1,2,3))
{
// A bunch of code here.
}
Run Code Online (Sandbox Code Playgroud)
为什么不只是声明变量并使用它?
这种使用语法似乎只是使代码混乱并使其可读性降低.如果重要的是那个变量只在那个范围内可用,为什么不把这个块放在一个函数中呢?
有没有办法获取cleint的主机名在WCF中调用net.tcp绑定.我正在尝试诊断一个问题,我想找出哪个客户端正在向我发送导致它的消息.
我试过了:
OperationContext.Current.Channel.RemoteAddress.Uri.AbsoluteUri
Run Code Online (Sandbox Code Playgroud)
但这似乎只给我一个通用架构而不是主机名/ IP地址.