简体   繁体   中英

C# user defined exception not being caught

I defined my custom exception like this:

[Serializable]
    public class WrongFileException : Exception
    {
        public WrongFileException()
        {

        }
        public WrongFileException(string message) : base(message)
        {

        }

        public WrongFileException(string message, Exception innerExeException) : base(message, innerExeException)
        {

        }
    }

I have a button that opens a file and I want to throw an exception when it's the wrong file:

private void PanelOpen_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog opendlg = new Microsoft.Win32.OpenFileDialog();
        opendlg.DefaultExt = ".xml";
        opendlg.Filter="XML Files|*.xml";

        Nullable<bool> result = opendlg.ShowDialog();

        if (result == true)
        {
            XmlSerializer XMLSerial = new XmlSerializer(typeof(List<Panel>));
            FileStream fsCheck = new FileStream(opendlg.FileName, FileMode.Open);
            XmlReader reader = new XmlTextReader(fsCheck);
            fsCheck.Close();
            if (!XMLSerial.CanDeserialize(reader))
            {
                throw (new WrongFileException("Wrong data sructure for PanelList"));
            }
            try
            {
                using (FileStream fsRead = new FileStream(opendlg.FileName, FileMode.Open, FileAccess.Read))
                {
                    PanelList = XMLSerial.Deserialize(fsRead) as List<Panel>;
                }
                PanelBoxListView.ItemsSource = PanelList;
                PanelBoxListView.Items.Refresh();
            }
            catch(WrongFileException q)
            {
                MessageBox.Show("WrongFileException: {0}", q.Message);
                throw;
            }
        }
        CheckForListEntries();
    }

Testing it trying to open with a file that won't work I get "WrongFileException was unhandled". I don't get why because I am catching the exception?

Your throw (new WrongFileException(...)); is before the try block, so the try - catch never becomes relevant.

It catches only if the exception is thrown within try block.

            try
            {
                if (!XMLSerial.CanDeserialize(reader))
                {
                    throw (new WrongFileException("Wrong data sructure for PanelList"));
                 }
                using (FileStream fsRead = new FileStream(opendlg.FileName, FileMode.Open, FileAccess.Read))
                {
                    PanelList = XMLSerial.Deserialize(fsRead) as List<Panel>;
                }
                PanelBoxListView.ItemsSource = PanelList;
                PanelBoxListView.Items.Refresh();
            }
            catch(WrongFileException q)
            {
                MessageBox.Show("WrongFileException: {0}", q.Message);
                //throw; throw only if you handle/log it down the track
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM