3

I need to separate database access error (like wrong username) from database query error (like select insert..column not exist etc.). They will both throw SqlException. But I want my program to continue when a query error is thrown but and shut down when an access error is thrown.

How can I separate these two errors? Write my own exception?

rikitikitik
  • 2,414
  • 2
  • 26
  • 37
LIU
  • 295
  • 1
  • 4
  • 11

5 Answers5

1

I suggest checking the Number propery of the SqlException.

See my answer to SqlException catch and handling

Community
  • 1
  • 1
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
0

Well the basic syntax for excption handling is given below and you can trap as may type as you want. this is the way to separate one exception from another.

try
{
    // Statements that are can cause exception
}
catch(Type x)
{
    // Statements to handle exception
}
finally
{
    // Statement to clean up 
}

also please check How to: Create User-Defined Exceptions and Exceptions and Exception Handling (C# Programming Guide) for better understanding of exception handling because from your question it seem you need to know better about those concepts so you can raise better questions.

Raise your question standrads to get better answer in this community and raise acceptance ratio as well.

If you are doing asp.net please also look for Exception handling in C# and ASP .Net to understand how to do Exception handling.

Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45
0

1 exception of wrong username and password can be catch using sqlconnection property and 2 exception can be catch using sqlexception

siddharth
  • 441
  • 3
  • 11
  • 20
0

Why don't you process the error message? That will let you know what error it was e.g:

try
{

}
catch(SqlException e)
{
    if(e.Message=="anything") //or, maybe e.Message.Contains("column")
     {
     }
    else{}
}

You can then do what you want!

naqvitalha
  • 793
  • 5
  • 9
  • 3
    It should probably be looking at e.Number / e.ErrorCode, but *other than that* this is the right idea... and the "else" should probably involve `throw;` (i.e. rethrow exceptions that we weren't happy to absorb) – Marc Gravell Jul 18 '12 at 05:58
  • There is no e.ErrorCode option available, is there a special way to get to it? I looked in Silverlight, WPF and WP7 projects. – naqvitalha Jul 18 '12 at 06:32
  • I see e.Data, e.HelpLink, e.Message, e.HResult, e.StackTrace, e.Source and e.InnerException in WPF. Which platform are you referring to? – naqvitalha Jul 18 '12 at 17:47
  • if you are catching SqlException (as per your example) you have more than that. It sounds to me like you are just catching Exception – Marc Gravell Jul 18 '12 at 18:57
0
try{
    // Do something
}

catch 
(IOException ix)
{
   // handle...
 }  

 catch (System.UnauthorizedAccessException ux)
 {
    // handle
 }

so this way you can handle access level exception. To cross check my answer you can verify at http://msdn.microsoft.com/en-us/library/system.unauthorizedaccessexception.aspx

NG.
  • 5,695
  • 2
  • 19
  • 30