The Right Approach to Exceptions

Juan Manuel Cabo juanmanuel.cabo at gmail.com
Mon Feb 20 15:46:56 PST 2012


On 02/20/2012 04:44 PM, Sean Kelly wrote:
> On Feb 20, 2012, at 11:44 AM, deadalnix wrote:
> 
>> That wouldn't work, because you'll erase the stacktrace.
> 
> It wouldn't be difficult to not overwrite a stack trace if one already exists on throw.

That would be very nice!!

Java doesn't have a stacktrace reset problem:

I tried the following in java. You can see by the output
that the stacktrace of the exception object is
preserved (I didn't leave blank lines on purpouse so you can
count the line number shown in the output unequivocally):

public class bla {
	public static void main(String[] args) throws Exception {
		anotherfunc();
	}
	public static void anotherfunc() throws Exception {
		try {
			System.out.println("another func");
			badfunc();
		} catch (Exception ex) {
			//rethrow the same exception:
			throw ex;
		}
	}
	public static void badfunc() throws Exception {
		System.out.println("bad func");
		throw new Exception("badfunc");
	}
}

another func
bad func
Exception in thread "main" java.lang.Exception: badfunc
        at bla.badfunc(bla.java:16)
        at bla.anotherfunc(bla.java:8)
        at bla.main(bla.java:3)

--jm



More information about the Digitalmars-d mailing list