0
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. Why?

1
  • And what are the type, message, and stack trace of the unhandled exception? Commented Oct 14, 2011 at 6:13

4 Answers 4

2

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" } 
Sign up to request clarification or add additional context in comments.

1 Comment

@senzacionale: Alright, so that rules out my second suggestion. But you're still throwing an exception (and you have not posted the code that catches it, so I assume it's not being caught). Wait -- are you expecting the exception to be uncaught, and wondering why it is getting caught, or are you expecting the exception to be caught, and wondering why it isn't?
1

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"); } 

3 Comments

You shouldn't pop a message box in a data access component accessing oracle - let it bubble up if needed and handle at an outer layer ...
@bryanmac - I agree, but we are not given any context here - who knows where this code sits? I'm simply answering what I think is the question ;-) Your answer definitely addresses wider issues with the code and I agree with your points there.
Good point - if it's gui layer directly accessing DB, then could be OK. Guess, that's a different design issue ... thx
1

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.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.