Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am using Exception Handling. while using the below code . it shows the error msg as below
C#
try
{

}
catch(Exception ex)
{
   throw ex;
}

Error msg is :

The type caught or thrown must be derived from System.Exception

please help me to solve this problem.

Thanks in Advance
Mohan
Posted
Updated 11-Sep-12 17:57pm
v2
Comments
Ashraff Ali Wahab 11-Sep-12 23:58pm    
Why a empty try block gives you exception.Please look at your question and supply the correct code.
Rickin Kane 12-Sep-12 0:03am    
can you paste the code you are doing in try { }
ledtech3 12-Sep-12 0:24am    
It looks like you are trying to throw and catch the same exception.
I normaly Code this way in VB.

Try

Catch ex As Exception
MsgBox(ex.Message)
End Try
Here is a converted version from VB to C#
try {
} catch (Exception ex) {
Interaction.MsgBox(ex.Message);
}

Hi Mohan,

The Error message displayed is fairly straight forward. This error would happen possibly when there are ambiguous declaration of "Exception" class. I would suggest you to use the complete namespace(prefix the Exception with System) in the catch block as below.

Solution:
C#
try
{

}
catch (System.Exception ex)
{
    throw ex;
}


Use the below script to reproduce the error scenario:

C#
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    public class Exception
    {

    }
}
 
Share this answer
 
Comments
Member 7781058 9-Oct-13 7:44am    
the Exception class you added below to the Form1 class does not have specific methods in the System.Exception Class. how can you say that, this will solve problem.
It seems like you have a custom exception handling, if so:

C#
try
{

}
catch(Exception ex)
{
   throw new System.Exception(ex.ToString()); // you may change to ex.Message if you have a property Message on your custom exception.
}
 
Share this answer
 
hi.... try using below code:

C#
try
{

}
catch(System.Exception ex)
{
   throw ex;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900