locked
Exception Message - pass to another page RRS feed

  • Question

  • User323302215 posted

    I have a public function that calls out and returns a set of values (if any) so I have set up a try, catch exception in order in order to catch any exceptions, I want to use the exception message and display it in a label on another page. How can I pass this exception message into a label on another page? In order so I can have the exception message displayed with my own material. What is the best way to go about this? I'm thinking of setting up a property in the Catch and using it that way. Any examples of pointers are very welcome. Thanks

     Example - Inside the public function:

    Try
    
    'logic in here that may cause an exception 
    
    Catch ex As Exception
    
    ex.Message 'pass this message of the exception so it can be used in a different page
    
    End Try

    Monday, May 6, 2013 5:43 AM

All replies

  • User-1716253493 posted
    Session["message"]=ex.Message;

    Monday, May 6, 2013 5:48 AM
  • User220175380 posted

    Hi,

    Use the session variable for this requirement. Assign the captured exception message in the Session object and pass it to next page

    Session("error")=ex.message

    Assign the error message to the lable in another page

    lblmsg.text=Session("error")

    Monday, May 6, 2013 5:51 AM
  • User1557244751 posted

    You can use below code to get your last exception in any page

    var exception = Server.GetLastError();

    Above code will give object of exception in variable exception so after that you can process as you wish.

    Monday, May 6, 2013 5:51 AM
  • User551462331 posted

    u can create a session variable for ExceptionMessage and whenever u get the exception, store it in session variable

    Session("ExceptionMessage") = ex.Message

    and then in differnt page, u can read exception message value... like this

    Dim strExceptionMsg as String = IIF(not Session("ExceptionMessage") = Nothing, Session("ExceptionMessage").ToString(), "")

    hoep this helps...

    Monday, May 6, 2013 5:53 AM
  • User323302215 posted

    When I add this code into the catch I get an error, cannot resolve Symbol Session. Do you know how could I resolve it?

    Try
    
    'logic in here that may cause an exception
    
    Catch ex As Exception
    
    Session("ExceptionMessage") = ex.Message 'get an error stating that 'Cannot resolve the symbol session'
    
    End Try

     

     

     

    Monday, May 6, 2013 6:03 AM
  • User551462331 posted

    When I add this code into the catch I get an error, cannot resolve Symbol Session. Do you know how could I resolve it?

    use

    HttpContext.Session("ExceptionMessage") = ex.Message

    hope this helps...

    Monday, May 6, 2013 6:05 AM
  • User323302215 posted

    Thanks for the help kedar, it's giving me the error "Error 1 Name 'HttpContext' is not declared". Is it possible to declare a session from inside the catch statement? 

    Monday, May 6, 2013 6:16 AM
  • User-1716253493 posted

    try

    Try
     'logic in here that may cause an exception
    Catch ex As Exception
     Dim exmessage as string = ex.Message 
     Session("ExceptionMessage") = exmessage 
    End Try



    Monday, May 6, 2013 6:22 AM
  • User323302215 posted

    Hi, thanks for your help, this solution makes sense and should work however the error, "Name 'Session' is not declared" is appearing, the try catch is inside a funtion in a namespace so i'm not sure why this isn't working

    Monday, May 6, 2013 6:35 AM
  • User-1716253493 posted

    What event you use?

    Monday, May 6, 2013 6:39 AM
  • User551462331 posted

    Hi, thanks for your help, this solution makes sense and should work however the error, "Name 'Session' is not declared" is appearing, the try catch is inside a funtion in a namespace so i'm not sure why this isn't working

    I suppose u r code is present in .aspx.vb page which inherits from web.ui.page

    also, make sure u have addred reference to

    Imports System.Web

    hope this helps...

    Monday, May 6, 2013 6:42 AM
  • User323302215 posted

    When I reference Imports System.Web, it throws errors for my ConfigurationManager, is there other way to do this without a session? Could it be done by a property so that it will catch the execption message, it will create it in memory, then add to the page_load method to add the message if it exists, thanks again

     

    Monday, May 6, 2013 7:02 AM