Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Exception on try-catch clause

Up to now, whenever I wanted to show an exception thrown from my code I used:

try
{
    // Code that may throw different exceptions
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());         
}

I used the above code mainly for debugging reasons, in order to see the exact type of exception and the according reason the exception was thrown.

In a project I am creating now, I use several try-catch clauses and I would like to display a popup message in case of an exception, to make it more "user friendly". By "user friendly", I mean a message that would hide phrases like Null Reference Exception or Argument Out Of Range Exception that are currently displayed with the above code.

However I still want to see relevant info with the type of exception that created the message.

Is there a way to format the displayed output of thrown exceptions according to previous needs?

like image 881
apomene Avatar asked Apr 22 '13 10:04

apomene


People also ask

How do I show exceptions in C#?

The catch clause contains the exception handler that just displays a message on the screen. When the throw statement is called from inside ProcessString , the system looks for the catch statement and displays the message Exception caught .

How do you handle exceptions in catch block?

You can put a try catch inside the catch block, or you can simply throw the exception again. Its better to have finally block with your try catch so that even if an exception occurs in the catch block, finally block code gets executed. Finally block may not get executed in certain exceptions.

Can you throw an exception in a catch block?

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects). Or, wrap it within a new exception and throw it.

How do you catch a specific exception?

The order of catch statements is important. Put catch blocks targeted to specific exceptions before a general exception catch block or the compiler might issue an error. The proper catch block is determined by matching the type of the exception to the name of the exception specified in the catch block.


2 Answers

You can use .Message, however I wouldn't recommend just catching Exception directly. Try catching multiple exceptions or explicitly state the exception and tailor the error message to the Exception type.

try 
{
   // Operations
} 
catch (ArgumentOutOfRangeException ex) 
{
   MessageBox.Show("The argument is out of range, please specify a valid argument");
}

Catching Exception is rather generic and can be deemed bad practice, as it maybe hiding bugs in your application.

You can also check the exception type and handle it accordingly by checking the Exception type:

try
{

} 
catch (Exception e) 
{
   if (e is ArgumentOutOfRangeException) 
   { 
      MessageBox.Show("Argument is out of range");
   } 
   else if (e is FormatException) 
   { 
      MessageBox.Show("Format Exception");
   } 
   else 
   {
      throw;
   }
}

Which would show a message box to the user if the Exception is an ArgumentOutOfRange or FormatException, otherwise it will rethrow the Exception (And keep the original stack trace).

like image 188
Darren Avatar answered Oct 15 '22 15:10

Darren


try
     {
        /////Code that  may throws several types of Exceptions
     }    
     catch (Exception ex)
       {
         MessageBox.Show(ex.Message);         
       }

Use above code.

Can also show custom error message as:

try
     {
        /////Code that  may throws several types of Exceptions
     }    
     catch (Exception ex)
       {
         MessageBox.Show("Custom Error Text "+ex.Message);         
       }

Additional :

For difference between ex.toString() and ex.Message follow:

Exception.Message vs Exception.ToString()

All The details with example:

http://www.dotnetperls.com/exception

like image 30
Freelancer Avatar answered Oct 15 '22 17:10

Freelancer