我有一个抛出自定义异常的方法。 我的自定义例外:
public class IllegalEntryException extends Exception
{
public IllegalEntryException(String message)
{
super(message);
}
}
和我的方法:
public Order(String[] value) throws IllegalEntryException
{
String timeString = value[0];
try
{
this.time = Integer.parseInt(timeString);
}
catch(NumberFormatException e)
{
throw new IllegalEntryException("First entry of each column, time must be int, found at row ");
}
}
现在在实际上调用此函数的方法(也是构造函数)上,我可以访问一个变量,该变量跟踪发生此错误的文件的行号。我想在引发异常时(即在构造函数中)在消息中添加此行号,以便最终在那儿处理它时也打印行号。
像这样:
try
{
calltoFunction(); // exception is thrown
// somehow add the row number to the message
}
catch {}//Handle the exception over here
我该如何实现?
答案 0 :(得分:0)
真的很抱歉,我正在寻找这样的东西:
try
{
Order order = new Order(values);
}
catch(IllegalEntryException e)
{
String error = e.getMessage() + "row_no";
System.err.println(error);
}
答案 1 :(得分:0)
引发异常时,可以通过调用以下内容来访问它的stacktrace(它还包含堆栈上的所有方法调用,包括它们的类名和引起异常的行):
uniform sampler2D tex[4];
vec4 YUVAtoRGBA()
{
mat4 xForm = mat4(
1.164, 1.164, 1.164, 0.0,
0.0, -0.391, 2.018, 0.0,
1.596, -0.813, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0);
vec4 yuva = vec4(
texture2D(tex[0], (uvOut.xy / uvOut.z).x - 0.0625,
texture2D(tex[1], (uvOut.xy / uvOut.z).x - 0.5,
texture2D(tex[2], (uvOut.xy / uvOut.z).x - 0.5,
texture2D(tex[3], (uvOut.xy / uvOut.z).x);
return colorVarying * (xForm * yuva);
}
此外,
e.getStackTrace();
会将所述堆栈跟踪信息打印到System.out流中,这对于调试目的就足够了。
我不知道通过在catch块中添加行号来实现什么目的,但是您可以尝试通过以下方式将整个堆栈跟踪作为字符串获取:
e.printStackTrace();
然后从字符串
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter );
String stackTrace = stringWriter.toString();
中找出行号。