Fraz Sundal
Fraz Sundal

Reputation: 10448

How to get Type of Exception in C#

I want to check if the server is not accessible and if its not accessible i want to print a friendly message on my login page. Like when user input its credential and in exception i got

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

this exception. So how should i when which exception is occurred so i can display a message?

Upvotes: 30

Views: 78608

Answers (8)

Shashank Shekhar
Shashank Shekhar

Reputation: 4178

try
{
    // Your code
}
catch (SQLException ex)
{
    Response.Write(ex.ToString());    // For web apps
    Console.WriteLine(ex.ToString()); // For Console apps
}

Upvotes: -1

parmarupendra
parmarupendra

Reputation: 83

I think, the solution is:

catch (Exception ex)
{
    Responce.Write("An error occured: " + ex.Message);
}

Upvotes: -3

John D Kidd Jr
John D Kidd Jr

Reputation: 663

I know this is an older post, but if you are going to handle all exceptions the same way and/or are using the information for error reports or something similar (instead of notifying the user of the specifics) you can use the following.

try
{
    //do something here
}
catch(Exception ex)
{
    MessageBox.Show(ex.GetType().ToString()); //will print System.NullReferenceException for example
}

Upvotes: 65

Gorlykio
Gorlykio

Reputation: 63

try  {
 //some code } catch(TypeOfException exOne)  {
 //handle TypeOfException someway } catch (OtherTypeOfException exTwo)  {
 //handle OtherTypeOfException some other way } catch (Exception ex)  {
 //handle unknown exceptions in a general way } finally  {
 //any required cleanup code goes here }

try/finally and using are almost the same. using will try to do something with the object, whether it succeeds or not it will dispose of the object to stop memory leaking, but will not ignore the error preventing the code from continuing.

Try will try to perform the code inside the braces, if an error occurs it will ignore the error and exit the Try clause, then proceed on reading code, unless the error is critical which causes the program to crash. However when using streams it wont ensure the stream is closed/ dispose of, because when an error occurs it exits the try clause before reaching any code you might of added at the bottom for disposing (bad practice imo).

Try code requires either a Catch(s), and/or Finally statment. Catch gives you the opportunity to handle exceptions, generally or specific exceptions. an example of use could be, to generate a textfile of the error and save it. using wouldn't let you handle exceptions.

another example of using could be, you have a playlist, a song cant be found, the exception area nullreferece might remove the item from the list.

Finally is always executed, even if the error is critical and the program crashes, the code inside the finally clause will be executed, with stream objects this is where you should be placing your dispose code, so that if the object fails in the try clause its always disposed of.

hope i've helped present some clarity regarding using and try/catch/finally.

Upvotes: 2

Baz1nga
Baz1nga

Reputation: 15579

you can use the same method that you use to check whether a parent class is of type subclass which is done using

 obj is NotImplementedException

where your obj is of type Exception the parent class of all exceptions.

or if you want to use the exception object later then you can use:

var niException=obj as NotImplementedException
if(niException==null)  //will be null when object is not of type NotImplementedException
return;

This logic is especially useful when you have a centralised class for handling exceptions and dont want to add multiple catch statements

Hope this helps.

Upvotes: 8

Johan Strömhielm
Johan Strömhielm

Reputation: 197

You need to know at code-time what exceptions to expect, in order to catch them accordingly. As Dimitrov stated, a SQLException is thrown when the connection to an SQL server fails, so catching that specifically is a good tactic.

You want to catch the various exceptions in order, like so:

try 
{
    //some code
}
catch(TypeOfException exOne) 
{
    //handle TypeOfException someway
}
catch (OtherTypeOfException exTwo) 
{
    //handle OtherTypeOfException some other way
}
catch (Exception ex) 
{
    //handle unknown exceptions in a general way
}
finally 
{
    //any required cleanup code goes here
}

Try to put the most unusual exceptions at the top, working your way down the list towards more common ones. The catch sequence is sequential - if you put catch(Exception) at the top, it will always catch on that line no matter what exceptions you code for beneath it.

Upvotes: 14

Oskar Kjellin
Oskar Kjellin

Reputation: 21870

You have to catch the exception:

try
{
      //Contact the server
}
catch(YourCouldNotContactServerException)
{
    //Show some friendly message
}

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

You could try catching a SQLException:

try 
{
    // Try sending a sample SQL query
} 
catch (SQLException ex) 
{
    // Print error message
}

Upvotes: 9

Related Questions