patch 9.1.0984: exception handling can be improved

Problem:  exception handling can be improved
Solution: add v:stacktrace and getstacktrace()

closes: #16360

Co-authored-by: Naruhiko Nishino <naru123456789@gmail.com>
Signed-off-by: ichizok <gclient.gaap@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/ex_eval.c b/src/ex_eval.c
index 79e9d94..e996ce2 100644
--- a/src/ex_eval.c
+++ b/src/ex_eval.c
@@ -562,6 +562,10 @@
 	excp->throw_lnum = SOURCING_LNUM;
     }
 
+    excp->stacktrace = stacktrace_create();
+    if (excp->stacktrace != NULL)
+	excp->stacktrace->lv_refcount = 1;
+
     if (p_verbose >= 13 || debug_break_level > 0)
     {
 	int	save_msg_silent = msg_silent;
@@ -647,6 +651,7 @@
     if (excp->type == ET_ERROR)
 	free_msglist(excp->messages);
     vim_free(excp->throw_name);
+    list_unref(excp->stacktrace);
     vim_free(excp);
 }
 
@@ -671,6 +676,7 @@
     excp->caught = caught_stack;
     caught_stack = excp;
     set_vim_var_string(VV_EXCEPTION, (char_u *)excp->value, -1);
+    set_vim_var_list(VV_STACKTRACE, excp->stacktrace);
     if (*excp->throw_name != NUL)
     {
 	if (excp->throw_lnum != 0)
@@ -721,6 +727,7 @@
     if (caught_stack != NULL)
     {
 	set_vim_var_string(VV_EXCEPTION, (char_u *)caught_stack->value, -1);
+	set_vim_var_list(VV_STACKTRACE, caught_stack->stacktrace);
 	if (*caught_stack->throw_name != NUL)
 	{
 	    if (caught_stack->throw_lnum != 0)
@@ -741,6 +748,7 @@
     {
 	set_vim_var_string(VV_EXCEPTION, NULL, -1);
 	set_vim_var_string(VV_THROWPOINT, NULL, -1);
+	set_vim_var_list(VV_STACKTRACE, NULL);
     }
 
     // Discard the exception, but use the finish message for 'verbose'.