Catch Specific MySQL-Error (C#) - c#

Is there way to catch a specific MySQL Error or more than one specific MySQL-Error?
I want to call a method if the error is "no selected database".
I am working with the MySQL-Connector / Net.

In your catch clause of your exception, you should be able to examine the Number property of the exception to determine which MySql exception was thrown. For example:
try
{
//Blah
}
catch (MySqlException ex)
{
if (ex.Number == 1046)
{
//Handle
}
throw;
}
catch (OtherExceptionType oet)
{
//Handle
}
If you are using C# 6.0, you can use exception filters.
try
{
//Blah
}
catch (MySqlException ex) when (ex.Number == 1046)
{
// Handle
}
catch (OtherExceptionType oet)
{
//Handle
}
The error number for "no database selected" is 1046 according to the documentation. Other error numbers are listed there as well.

Related

What is the recommended way to catch exceptions

I have to do a code review and i got to a code part that addresses possibles exceptions. it looks to me that the developer coding works but i want to ask what is the usual and correct way to do that. What is the best way to do catch exceptions?
the coder wrote:
try
{ . . . }
catch (Exception ex)
{
if (ex is PlatformNotSupportedException)
{ //for the Windows version or edition that does not support.
// tracing
}
else if (ex is NotSupportedException || ex is IOException)
{ // for the NTFS not supported or EFS is not configured
// tracing
}
else
{
//report any exception as encrypt/decrypt
}
}
I thought that the book says that it should be:
catch (PlatformNotSupportedException pnse)
{
//for the Windows version or edition that does not support.
// tracing
}
catch (NotSupportedException nse)
{
// for the NTFS not supported or EFS is not configured
// tracing
}
catch (IOException ioe)
{
// tracing for IOE
}
catch (Exception e)
{
//report any exception as encrypt/decrypt
}
The second approach would be more preferred. However, there is tiny difference between proposed solution and current one. You'd need to refactor to a method, or copy the code in two places (NotSupportedException and IOException catch blocks), whilst current implementation handles it under the same if block.
So, if you want to follow the same approach, you can use when keyword to filter out certain types and more.
catch (PlatformNotSupportedException pnse)
{
// for the Windows version or edition that does not support.
// tracing
}
catch (Exception ex) when (ex is NotSupportedException || ex is IOException)
{
// for the NTFS not supported or EFS is not configured
// tracing
}
catch (Exception e)
{
//report any exception as encrypt/decrypt
}
If that's not mandatory, you can leave implementation as is
TLDR: Use the second form so that the compiler catches ordering errors.
The reason that you should use the second form is because then you will get a compile error if you attempt to handle the types in the wrong order.
For example, this will give you an actual compiler error:
try
{
throw new ArgumentOutOfRangeException();
}
catch (Exception)
{
Console.WriteLine("Caught 'Exception'");
}
// This gives a compile error:
// "Error CS0160 A previous catch clause already catches all exceptions of this or of a super type ('Exception')"
catch (SystemException)
{
Console.WriteLine("Caught 'SystemException'");
}
However, using if/else if will NOT cause a compile error, so the error goes unnoticed:
try
{
throw new ArgumentOutOfRangeException();
}
catch (Exception ex)
{
if (ex is Exception)
{
Console.WriteLine("Caught 'Exception'");
}
else if (ex is SystemException) // This will never be reached, but no compile error.
{
Console.WriteLine("Caught 'SystemException'");
}
}
Note, however, that tools such as Resharper will warn you for the second case.
this would be generic for all type of exception
try
{
.....code
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

Different catch in try-catch

What is difference between these types of catch, except that in first I can use e?
catch (Exception e)
{
//some code;
}
catch (Exception)
{
//some code;
}
catch
{
//some code;
}
try{
//do something
}catch{
//do something
}
This catch is executed, regardless of the exception.
try{
//do something
}catch (Exception) {
//do something
}
This catch is executed when a specific Exception is thrown
try{
//do something
}catch (Exception e) {
//do something
}
Same here, only that you have a reference to the Exception. That way, you have access to it.
Read more here.
Catch can catch different exception's types.
When you use the syntax catch(Exception) you are telling the compiler to write code that catches any kind of exceptions while, if you use a syntax like catch(InvalidOperationException), you are asking to catch a specific type of exception
To simplify things you can write catch without any type and this has the same meaning of catch(Exception)
try
{
// Uncomment this line to catch the generic exception
// throw new Exception("An exception occurred");
throw new InvalidOperationException("Operation x is not valid in this context");
}
// Comment the following lines to fall into the generic catch exception
catch (InvalidOperationException)
{
// But without the variable we cannot print out the message....
Console.WriteLine("An invalid operation has been catched");
}
catch (Exception)
{
Console.WriteLine("An exception raised");
}
You cannot use the syntax catch(Exception ex) in the same try catch where you don't specify the name of the variable for the same type of exception.
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// Syntax error: CS0160: A previous catch clause already catches ......
catch (Exception)
{
Console.WriteLine("An exception raised");
}
Strangely enough this doesn't result in a syntax error, but in a simple warning
catch(Exception)
{
....
}
// Warning CS1058: A previous catch clause already catches ......
catch
{
....
}
Of course you shouldn't catch exceptions that you are not prepared to handle. If you do it just to expose a message you risk the correct functionality of your program. Usually you catch only specific exceptions that you are know how to handle to allow your program to continue. The only reason that I could find to catch all exceptions is when you write down the exception data in some kind of log file and then throw again the exception.
catch(Exception ex)
{
Logger.Error("Unexpected exception", ex);
throw; // NEVER throw ex;
}
Remember that it is really never required to write throw ex because you loose the stack trace of the exception and make very difficult to track down the exact error point.
See: Best practices for catching and re-throwing .NET exceptions
If your code throws an exception, then the catch Block will be thrown and you have access to it over e.
catch (Exception e)
{
//some code;
}
If your code throws an exception, then the catch Block will be thrown indepented from the exception type and you don’t have access to it.
catch
{
//some code;
}
If your code throws an exception, then the catch Block will be thrown depending from the exception type and you don’t have access to it.
catch (Exception)
{
//some code;
}
Instead of Exception you should use a more specific exception type!
let's check
in this code you can write e.Message for check Catch Message
catch (Exception e)
{
Console.WriteLine("Error Message is : " + e.Message);
}
but in this you just skip From Exception (All Exceptions) and you can add more Exceptions
catch (sqlExcetion)
{
//if your code have sqlEsception Get here
}
catch (Exception)
{
//if your code have any Exception Get here
}
and in this code you can create one catch and all catch go this
catch
{
//all catch get here
}
The minor difference between:
try{
//do something
}catch (Exception) {
//do something
}
and
try{
//do something
}catch (Exception e) {
//do something
}
is: (the second one will give)
The variable 'e' is declared but never used
Also, if the code is like this:
catch(Exception e) { throw e; }
the original stacktrace is gone. So, you have to do: catch(Exception e) { throw; }
to see the original stacktrace.

How to handle exception from specific database error

I am trying to create a transaction like so:
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,
options))
{
try
{
dbContext.MyTable.PartnerId = someGuid;
dbContext.SaveChanges();
scope.Complete();
dbContext.AcceptAllChanges()
}
catch (Exception ex)
{
log.LogMessageToFile("Exception - ExceptionType: " +
ex.GetType().ToString() + "Exception Messsage: " + ex.Message);
}
}
I know if I try to insert an item manully in sql with a duplicate in a specific column, I get the following error from sql:
Cannot insert duplicate key row in object 'dbo.MyTable' with unique index 'idx_PartnerId_notnull'. The duplicate key value is (7b072640-ca81-4513-a425-02bb3394dfad).
How can I programatically catch this exception specifically, so I can act upon it.
This is the constraint I put on my column:
CREATE UNIQUE NONCLUSTERED INDEX idx_yourcolumn_notnull
ON YourTable(yourcolumn)
WHERE yourcolumn IS NOT NULL;
Try this:
try {
}
catch (SqlException sqlEx) {
}
catch (Exception ex) {
}
SQL errors and warnings that happen on the server side are caught in this exception.
Read about it here:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception(v=vs.110).aspx
The above answer would allow you to catch the SqlException, but you would need to further refine the handling within the 'SqlException' catch block if you only want to inform the user of a particular error. The SqlException class has a property of 'ErrorCode' from which you can derive the actual error being produced by the server. Try doing something like below:
try
{
}
catch (SqlException sqlEx)
{
if(sqlEx.ErrorCode == 2601)
{
handleDuplicateKeyException();
}
}
2601 is the actual error code produced by SQL Server for you particular error. For a full list just run the SQL:
SELECT * FROM sys.messages
Use SqlException's number property.
For duplicate error the number is 2601.
catch (SqlException e)
{
switch (e.Number)
{
case 2601:
// Do something.
break;
default:
throw;
}
}
List of error codes
SELECT * FROM sysmessages
You can catch it by its type:
try
{
// ...
}
catch (SpecialException ex)
{
}
catch (Exception ex)
{
}
EDIT: According to Ivan G's answer, you will get an SqlException, which has an error ErrorCode property that probably specific. So you have to check the error code for this type of error.
you can check exception text or it's other parameters when it is thrown, so then you can act like you wan conditionally
like :
catch(SqlException ex)
{
if(ex.Message.Contains("Cannot insert duplicate key row in object"))
{
}
}
or exception number like
catch(SqlException ex)
{
switch (ex.Number)
{
case : someNumber:
{
//..do something
break...;
}
}
}

How to determine if an exception is of a particular type

I have a piece of try catch code:
try
{
...
}
catch(Exception ex)
{
ModelState.AddModelError(
"duplicateInvoiceNumberOrganisation", "The combination of organisation and invoice number must be unique");
}
For this piece of code I'm trying to insert a record into a database: The dba has set it up so that the database checks for duplicates and returns an error if there are duplicates. Currently, as you can see, I'm adding the same error to the model no matter what error occurred. I want it changed so this error is only added to the model if it was caused by the duplicate error set up by the dba.
Below is the error I want to catch. Note it's in the inner exception. Can anyone tell me how to specifically catch this one?
before your current catch add the following:
catch(DbUpdateException ex)
{
if(ex.InnerException is UpdateException)
{
// do what you want with ex.InnerException...
}
}
From C# 6, you can do the following:
catch(DbUpdateException ex) when (ex.InnerException is UpdateException)
{
// do what you want with ex.InnerException...
}
Replace System.Threading.ThreadAbortException with your exception.
try
{
//assume ThreadAbortException occurs here
}
catch (Exception ex)
{
if (ex.GetType().IsAssignableFrom(typeof(System.Threading.ThreadAbortException)))
{
//what you want to do when ThreadAbortException occurs
}
else
{
//do when other exceptions occur
}
}
Not enough rep to comment. In response to #conterio question (in #Davide Piras answer):
is there a catch "when not" syntax?
There is.
catch (Exception e) when (!(e is ArgumentException)) { }
To get name of the exception you can use
catch (Exception exc){
if (exc.GetType().FullName == "Your_Exception")
{
// The same can be user for InnerExceptions
// exc.InnerException.GetType().FullName
}
}
You can take a look at the SQLException class -- and check for the contents of the exception's message if it contains what you now see in your inner exception..Something like this:
try
{
//your code here
}
catch (SQLException ex)
{
if (ex.Message.Contains("Cannot insert duplicate key in obj...."))
{
//your code here
}
}

Catch SqlException when Attempting NHibernate Transaction

I need to see an errorcode produced by a SqlException - however, I can't get one to fire. I use NHibernate and have a SQL UNIQUE CONSTRAINT setup on my table. When that constraint is violated, I need to catch the error code and produce a user-friendly message based off of that. Here is a sample of my try/catch:
using (var txn = NHibernateSession.Current.BeginTransaction()) {
try {
Session["Report"] = report;
_reportRepository.SaveOrUpdate(report);
txn.Commit();
Fetch(null, report.ReportId, string.Empty);
} catch (SqlException sqlE) {
var test = sqlE.ErrorCode;
ModelState.AddModelError("name", Constants.ErrorMessages.InvalidReportName);
return Fetch(report, report.ReportId, true.ToString());
} catch (InvalidStateException ex) {
txn.Rollback();
ModelState.AddModelErrorsFrom(ex, "report.");
} catch (Exception e) {
txn.Rollback();
ModelState.AddModelError(String.Empty, Constants.ErrorMessages.GeneralSaving);
}
}
Please pardon my ignorance.
Check this out which illustrates how to catch a GenericADOException and look at the InnerException property:
catch (GenericADOException ex)
{
txn.Rollback();
var sql = ex.InnerException as SqlException;
if (sql != null && sql.Number == 2601)
{
// Here's where to handle the unique constraint violation
}
...
}
Instead of catching a SqlException directly in your controller, I'd set up a SQLExceptionConverter to translate it to a more meaningful exception.

Categories