-6

I am working on a windows form application. when i run the project in one of my class error occurs. (see below image)

enter image description here

But when i press continue button The project runs well. Also, I can not delete this line of code. How can I ignore this error to continue project?

hamid
  • 59
  • 2
  • 9
  • 4
    Dont ignore it. Fix it. In the old days that error could result in the BSOD – Ňɏssa Pøngjǣrdenlarp Sep 23 '16 at 16:19
  • 1
    Generally you shouldn't ignore errors, they're an error for a reason. Something is broken, fix it. If you want to handle it or even ignore it then look at `try/catch' blocks. – Equalsk Sep 23 '16 at 16:19
  • I tried so hard to fix this error but did not. That's why I'm going to ignore – hamid Sep 23 '16 at 16:21
  • 1
    If you can absolutely guarantee that the rest of the program will run fine without this part of the code working successfully then go for it. It's still a terrible idea. – Equalsk Sep 23 '16 at 16:22
  • 2
    @hamid - You fail to realize the severity of this error. An unbalanced stack means your application is in an unstable state, and can cause all kinds of random problems further down the road. If you ignore this problem now, you will have problems later, and not be able to trace it back to this issue. – Erik Funkenbusch Sep 23 '16 at 16:24

3 Answers3

2

Use try-catch:

try {
    // code here
} catch (Exception) {
    // do something or nothing if caught
}

Or, if you want to catch a specified exception, do this:

try {
    // code here
} catch (/* exception class here */) {
    // do something or nothing if caught
}

For instance, if you want to catch NullReferenceException, then do this:

try {
    // code here
} catch (NullReferenceException) {
    // do something or nothing if caught
}

If you want to use exception data, define exception as a variable, like this:

try {
    // code here
} catch (Exception e) {
    // do something or nothing if caught
}

In Visual Studio, you can insert a try-catch snippet by typing try and double-hitting Tab key.

There's also try-catch-finally. Example:

try {
    // code here
} catch (Exception) {
    // do something or nothing if caught
} finally {
    // perform some cleanup here
}

In Visual Studio, you can type tryf and double-hit Tab key to insert a try-catch-finally snippet.

You can also just perform some cleanup using try-finally without getting any error:

try {
    // code here
} finally {
    // perform some cleanup here
}

More info on MSDN about try-catch, try-finally and try-catch-finally.

But still, if an error occurs, that mean something is wrong. Google some information about it.

0

You can try using trycatch :

try{
//your code
}
catch(Exception ex)
{
//Log the exception 'ex'
}
rm -rf .
  • 473
  • 1
  • 4
  • 14
0

You are looking for TryCatch handling:

// some code

try
{
    // "try" some code here 
    // You may put the line that may cause an error here

} catch(Exception ex)
{
    // This part will get executed if above did not work (error - threw an exception)
    // You may just keep it empty -> error will be ignored
    // or log `ex` information
    // or do something anything else
}

// control will continue - no crash
// some other code

For more information, Read C# - Exception Handling.

Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104