Public Group

exceptions.

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

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 on other sites
Quote:
 Original post by andrei999how 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.");}

Share on other sites
ok many thanks for this :)

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• Forum Statistics

• Total Topics
633659
• Total Posts
3013209
×