TheSwamp

Code Red => .NET => Topic started by: shers on October 15, 2015, 12:46:34 PM

Title: Exception Class
Post by: shers on October 15, 2015, 12:46:34 PM
Hi,

I have an Exception Class as below.

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class MyException : Exception
    {
    }
}

I call this class as below

Code: [Select]
if (result == false)
            {
                throw new MyException();
            }

But I get an error that says 'Unhandled exception has occured in a component in your application. If you click Continue, the application will ignore this error and attempt to continue.

Exception of type Test.MyException was thrown.

Please help.

Thanks
Title: Re: Exception Class
Post by: Kerry on October 15, 2015, 01:05:21 PM

I believe you'll need to add constructors to the class

eg
https://msdn.microsoft.com/en-us/library/87cdya3t(v=vs.110).aspx
Title: Re: Exception Class
Post by: Kerry on October 15, 2015, 01:08:16 PM

I call this class as below

Code: [Select]
if (result == false)
            {
                throw new MyException();
            }


every time you do that Zod kills a little kitten.

perhaps try something like :
Code: [Select]
if (!result)
            {
                throw new MyException();
            }
Title: Re: Exception Class
Post by: shers on October 15, 2015, 01:24:16 PM

I believe you'll need to add constructors to the class

eg
https://msdn.microsoft.com/en-us/library/87cdya3t(v=vs.110).aspx

That doesn't make any difference.
Title: Re: Exception Class
Post by: Keith Brown on October 15, 2015, 01:25:56 PM
You need to catch the exception once you throw it.
Title: Re: Exception Class
Post by: shers on October 15, 2015, 01:27:53 PM
It was a way to exit the application from the class. Any other option?
Title: Re: Exception Class
Post by: Keith Brown on October 15, 2015, 01:31:01 PM
You need to handle all exceptions.  Otherwise you will get an unhandled exception error.

Catch it in your calling method.
Title: Re: Exception Class
Post by: shers on October 15, 2015, 01:43:24 PM
try
            {
                if (!result)
                {
                    throw new MyException();
                }
            }
            catch (Exception ex)
            {
                throw;
            }

Still the same. I don't want the execution to continue when throw new MyException is reached. That is, the execution should stop. Is there any other method.
Title: Re: Exception Class
Post by: MexicanCustard on October 15, 2015, 01:51:11 PM
try
            {
                if (!result)
                {
                    throw new MyException();
                }
            }
            catch (Exception ex)
            {
                throw;
            }

Still the same. I don't want the execution to continue when throw new MyException is reached. That is, the execution should stop. Is there any other method.


You're just re-throwing the exception within the catch.  Whats catching your exception above this?  If you keep throwing the exception it will bubble up to the operating system and you get the error dialog you posted.  If you put a breakpoint within your catch clause you should be hitting it.
Title: Re: Exception Class
Post by: shers on October 15, 2015, 01:54:40 PM
Exception of type 'Test.MyException' was thrown. This is the error I get in the catch block
Title: Re: Exception Class
Post by: gile on October 15, 2015, 01:55:21 PM
Hi,

What's the issue ?
You throw an exception and the host application (AutoCAD here) alerts you an exception occured. This is the way all exception work.
Title: Re: Exception Class
Post by: Kerry on October 15, 2015, 01:59:00 PM

Oh, wait !

You don't really want an exception thrown ; You want to return to main and exit if the test returns true ?? Yes ?
Title: Re: Exception Class
Post by: shers on October 15, 2015, 01:59:46 PM
Exactly! I call a wpf window with an OK and Cancel button, from a class. When the user clicks on the Cancel button, the code should exit. Instead, it continues to execute. To avoid this, I call this MyException class, so that it simple exits. Is there any other way to quit the application if the user clicks on the Cancel button?

Thanks
Title: Re: Exception Class
Post by: Kerry on October 15, 2015, 02:03:32 PM

Yes, just close the wpf and return to main.
Title: Re: Exception Class
Post by: MexicanCustard on October 15, 2015, 02:10:12 PM
Exactly! I call a wpf window with an OK and Cancel button, from a class. When the user clicks on the Cancel button, the code should exit. Instead, it continues to execute. To avoid this, I call this MyException class, so that it simple exits. Is there any other way to quit the application if the user clicks on the Cancel button?

Thanks

Set the IsCancel property on your cancel Button to true that will handle the cancel button.  Unless you need to do some cleanup within the window, which you shouldn't be doing.

For the Ok button set the windows DialogResult property to true that takes care of that button.
Title: Re: Exception Class
Post by: MexicanCustard on October 15, 2015, 02:16:19 PM
This is the most basic example I can create explaining my previous post.

In your XAML:
Code: [Select]
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Width="75" Content="Cancel" Margin="5" IsCancel="True"/>
            <Button Width="75" Content="OK" Margin="5" IsDefault="True" Click="OkClicked"/>
</StackPanel>

In your code behind:
Code - C#: [Select]
  1. private void OkClicked(object sender, RoutedEventArgs e)
  2. {
  3.     DialogResult = true;
  4. }

Code - C#: [Select]
  1. var window = new myWindow();
  2. var result = window.ShowDialog();
  3. if (!result) //Return if they clicked Cancel or Closed the window
  4.     return;
  5.  
  6. //Do something if they clicked OK
  7.  
Title: Re: Exception Class
Post by: Keith Brown on October 15, 2015, 02:27:08 PM
every time you do that Zod kills a little kitten.


Made me laugh.  ;D ;D
Title: Re: Exception Class
Post by: Kerry on October 15, 2015, 10:08:26 PM
every time you do that Zod kills a little kitten.


Made me laugh.  ;D ;D

Yep, Me too :)

Title: Re: Exception Class
Post by: shers on October 18, 2015, 11:50:59 PM
Thanks all! It's solved. I had to handle it in the main.