Jump to content
  • Advertisement
Sign in to follow this  
andrei999

exceptions.

This topic is 3929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, In C#, custom made exceptions can be used with try/catch? for example:
public class StudentNotFoundException:Exception
{
  public StudentNotFoundException()
  {
    :base("No such student.")
  }
}
when I make a new such exception i need to say:
if (whatever)
{
throw new StudentNotFoundException()
}
how would i use the StudentNotFoundException with try/catch? also a second question about exceptions, whats the point in providing no argument in the catch? like this:
try
{
blabla code
}

catch{}
what exactly is done when an exception appears in this case? thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by andrei999
how would i use the StudentNotFoundException with try/catch?


Exactly as you would a normal exception.

Quote:
also a second question about exceptions, whats the point in providing no argument in the catch? like this:

try
{
blabla code
}

catch{}

what exactly is done when an exception appears in this case?


try
{
// ...
}
catch (FileNotFoundException ex)
{
/* Here, we catch a specific type of exception. This means that this code
will only execute if an exception of type FileNotFoundException is thrown.
This means that we can do any error handling that is specific to a file
not being found, as well as handle any properties that are specific to the
derived FileNotFoundException class (e.g. the FileName property used
below). */

Console.WriteLine("File " + ex.FileName + " could not be found.");
}

try
{
// ...
}
catch (Exception ex)
{
/* Here, we catch the base exception type -- all exceptions should inherit
from this class (e.g. FileNotFoundException is derived from the Exception
class) and so we should catch all exceptions. This means we can only use
generic methods and properties common to all exceptions. */

Console.WriteLine("An exception occured. Message:");
Console.Write(ex.Message);
}

try
{
// ...
}
catch (FileNotFoundException)
{
/* Here, we catch a type of exception -- but we don't give the instance
caught a name. This means that we can't get any information more specific
about the exception than that given by its type. */

Console.WriteLine("A file could not be found.");
}

try
{
// ...
}
catch
{
/* Here, we catch absolutely anything. This is because it is possible
(although rather poorly advised) to throw all sorts of things other than
'real' exceptions. We could have been thrown a string (although we could
choose, equally, to catch only strings in exactly the same way we catch
those 'real' exceptions). We know absolutely nothing at all about what
we're given. You might use this if any error at all is fatal -- you'll
inform the user and perform any further clean up necessary beyond that in
the 'finally' block which you may need to include. On the other hand, if
there's nothing that you can do about an exception, it may sometimes be
worth simply allowing it to propagate until it reaches something that can
handle it meaningfully. */

Console.WriteLine("A fatal exception occurred. This application will now terminate.");
}



Further reading at MSDN

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!