Author Topic: Exception Class  (Read 4605 times)

0 Members and 1 Guest are viewing this topic.

shers

  • Guest
Exception Class
« 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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Exception Class
« Reply #1 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
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Exception Class
« Reply #2 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();
            }
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

shers

  • Guest
Re: Exception Class
« Reply #3 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.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Exception Class
« Reply #4 on: October 15, 2015, 01:25:56 PM »
You need to catch the exception once you throw it.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

shers

  • Guest
Re: Exception Class
« Reply #5 on: October 15, 2015, 01:27:53 PM »
It was a way to exit the application from the class. Any other option?

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Exception Class
« Reply #6 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.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

shers

  • Guest
Re: Exception Class
« Reply #7 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.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Exception Class
« Reply #8 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.
Revit 2019, AMEP 2019 64bit Win 10

shers

  • Guest
Re: Exception Class
« Reply #9 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

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Exception Class
« Reply #10 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.
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Exception Class
« Reply #11 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 ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

shers

  • Guest
Re: Exception Class
« Reply #12 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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Exception Class
« Reply #13 on: October 15, 2015, 02:03:32 PM »

Yes, just close the wpf and return to main.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Exception Class
« Reply #14 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.
Revit 2019, AMEP 2019 64bit Win 10