开发者

Unhandled exception when calling throw

开发者 https://www.devze.com 2023-04-13 03:02 出处:网络
catch (OracleException e) { Cursor.Current = Cursors.Default; _instance = null; if (e.ErrorCode == -2147483648) // {\"ORA-01017: invalid username/password; logon denied\"}
catch (OracleException e)
{
    Cursor.Current = Cursors.Default;
    _instance = null;

    if (e.ErrorCode == -2147483648) // {"ORA-01017: invalid username/password; logon denied"}
    {
        throw new Exception("Nepravilno ime uporabnika ali geslo");
    }
    else
    {
        throw new Exception("Ne morem se povezati na podatkovno bazo. Preveri povezavo!");      
    }
}

but i always get Unhandled exception. W开发者_StackOverflow中文版hy?


At the risk of stating the obvious... Because you're not catching the Exception you throw in your catch block? Or, perhaps, something else is being thrown in the try block that isn't an OracleException.

What are you expecting to happen?

Just to be totally clear (to make sure that we're on the same page), an exception that's thrown but never caught will result in an unhandled exception (by definition). Throwing an exception from within a catch block is identical to throwing it from anywhere else; there still needs to be a try-catch somewhere to catch it. For example, this exception will be caught:

try {
    throw new Exception("Out of cheese error");    // Caught below
}
catch (Exception) {   }

But this one results in a new exception being propogated:

try {
    throw new Exception("Out of cheese error");    // Caught below
}
catch (Exception) {
    throw new Exception("418: I'm a teapot");    // Never caught
}

And this code catches both exceptions:

try {
    try {
        throw new Exception("Out of cheese error");    // Caught in inner catch
    }
    catch (Exception) {
        throw new Exception("418: I'm a teapot");     // Caught in outer catch
    }
}
catch (Exception e) {
    Console.WriteLine(e.Message);    // "418: I'm a teapot"
}


Your code does not in anyway swallow an exception. All it does is catch one type of exception and throw another type of exception. If you have an unhandled exception before you write this code, you will still have one after you write it.

--UPDATE --

Referring to your comment to another answer, if you want to display a message and stop executing code then try:-

catch (OracleException e)
{
    Cursor.Current = Cursors.Default;
    _instance = null;

    if (e.ErrorCode == -2147483648) // {"ORA-01017: invalid username/password; logon denied"}
    {
        MessageBox.Show("Nepravilno ime uporabnika ali geslo");
    }
    else
    {
        MessageBox.Show("Ne morem se povezati na podatkovno bazo. Preveri povezavo!");      
    }

    // this exits the program - you can also take other appropriate action here
    Environment.FailFast("Exiting because of blah blah blah");
}


I assume you call hierarchy look like this:

Main
    |-YourMethod
                try {}
                catch (OracleException) {throw new Exception("blah blah")}

So you see, the OracleException which occured in YourMethod is being caught by catch block, but then you throw a new one which goes into Main, where nothing handles it. So you should add an exception handler on the previous level.

Also, do not hide the original OracleException, throw your exception this way throw new Exception("your message", e). This will preserve the call stack.


Because you're only handling the OracleException. Nothing is handling the Exception() you are throwing.

You're catching the OracleException which means you're prepared to handle it - what does handling it mean to you? Logging it and moving on? Setting some state and moving on? Surely, you don't want to pop up gui in a data access component right? If you're not prepared to handle it, let it bubble up and handle it at an outer layer.

You also shouldn't throw exceptions of type Exception. Create your own strongly typed exceptions so they can be handled, or, simply log and call throw; which rethrows the original.

If you throw a new type of exception ensure you're passing the original exception as the inner exception to ensure you're not hiding details.

I did a write up on some best practices with C# exceptions:

Trying to understand exceptions in C#

Hope that helps

0

精彩评论

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

关注公众号