开发者

Can I ignore error without using try catch()?

So I want to now if that possible for .NET languages to ignore error without using try catch construction ?

I can ignore warring with #nowarring 40 for example, can I ignore error ?

Why I want it ?

simply wanted to call system pause with this way

open System.Runtime.InteropServices
[<DllImport(@"msvcrt.dll")>]
extern void system(string str)
system "pause"

but got Error message

unbalanced stack. This is probably because the managed PInvoke signature does not match the unmanaged target signature. Make sure that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

So I don't care, it works. But can I just ignore this error ? without doing weird stuff like that :

try (system "pause") catch |_->()

Ok , I solved my problem by adding CallingConvention=CallingConvention.Cdecl, but what the question was about skipping exceptions, so and I st开发者_StackOverflow社区ill don't know if I can do it.

So maybe I need to tell some real reason to not be down-voted hard - sometimes it's matters for program to live even with hard errors occupations, but even sometimes you don't need to catch them. You need just ignore them...

off topic : proof image about using void instead of int :

Can I ignore error without using try catch()?


Errors are there for a reason which should usually not be ignored. Your reason is that you are not using correct calling convention. See this answer for more information.

As for compile and runtime errors Graham has already pointed out the difference. If you get an unhandled runtime exception that usually means that your program is in an inconsistent state. If program would be allowed to continue there is no guarantee as to what would happen. It is better for a program to die than to proceed with corrupted data. You can have a look at AppDomain.UnhandledException event, but it only allows you to log the exception and do some cleaning, program will then exit anyway. There may be other options (depending on your programming language) but it is definitely not advisable to use them.


  • In C# I don't think there is a way to ignore all exceptions, apart from try ... catch
  • In VB.Net there is a way to ignore all exceptions:

VB.Net code:

On Error Resume Next '' all exceptions after this point will be ignored  
System("pause")

But it's a dangerous feature. Exceptions should only be ignored if

  1. You are absolutely confident that you understand why they have occurred
  2. You are absolutely confident that they can be safely ignored.

In my humble opinion, and with no offence meant, from your question I don't think you understand why the error has occurred or why it is dangerous to ignore it.


No. When you talk about ignoring warnings, these are compiler warnings. You can ignore these if you want, but they're generally warning you about something for a good reason. You can't ignore compiler errors, because when these occur, your code hasn't compiled and you have no binary to run.

When you talk about errors in your question, you're actually referring to exceptions. These are thrown at runtime. The only way to "ignore" them is to have a try/catch block which catches an exception and does nothing with it.

Also, if you really need to ignore an exception, it's best to catch the specific exception you want to ignore, rather than any exception. So, instead of something like this:

try
{
   ...
}
catch
{
}

or

try
{
   ...
}
catch(Exception)
{
}

you should try to use

try
{
   ...
}
catch(SpecificExceptionType)
{
}


Why don't you declare it like this:

extern int system(string str)

Your compiler is telling you something important here: It is telling you that the system function returns an int and that it doesn't know what to do with it. You have to make sure your signature matches!


There is no built in way to ignore exceptions, but you could define your own function to do so:

let ignoreError func defaultValue =
    try func() with _ -> defaultValue


Or you could simply turn off the MDA.


One approach is to create a method that takes a System.Action object and pass a lambda expression to execute, then whenever you want to execute something and ignore all errors just wrap this method call around it. This is shown below.

    public void IgnoreError(System.Action action)
    {
        try
        {
            action.Invoke();
        }
        catch
        {
        }
    }

    public void SomeMethod()
    {
        IgnoreError(() => { system "pause"; });
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜