473,323 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Error Handler...

Samishii23
246 100+
So I'm making a method to get the Error Type, and display a message accordingly based on that...

Expand|Select|Wrap|Line Numbers
  1. switch (exception) {
  2. case FileNotFoundException:
  3. // do stuff...
  4. break;
Error: FileNotFoundException is a 'type' used like a 'variable'
Dec 6 '09 #1

✓ answered by GaryTexmo

Hmm, where to start. Ok, the first thing I'll say is you can't do what you're trying to do... I'll carry on with explaining why but sorry if it seems a bit disjointed.

To address your immediate question, you are getting that error message because you're using a type like a variable, just like the error message says. A switch statement is expecting a value, not a type. If you want that to work, you'd need to change it to...

Expand|Select|Wrap|Line Numbers
  1. switch (e.GetType())
  2. {
  3.     case typeof(FileNotFoundException):
  4.         // do stuff
  5.         break;
  6. }
Now, that still won't compile because a switch statement can only work on constant values of an integral type (things like int, byte, etc... strings count here too). So what you'd need to do is change that switch statement to an if-else-if-else statement, as such...

Expand|Select|Wrap|Line Numbers
  1. if (e.GetType() == typeof(FileNotFoundException))
  2. {
  3.     // do stuff
  4. }
  5. else if (e.GetType() == <exception type>)
  6. {
  7.     // do stuff
  8. }
  9. else
  10. {
  11.     // do stuff
  12. }
Now, you can do this right as a part of your try/catch block, so you don't really need to set this up yourself, you just catch the exception you want and handle it accordingly. The caveat here is that you have to organize them properly, you catch the specific exceptions first, then the generic exceptions. So, for example...

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.  
  4. }
  5. catch (FileNotFoundException e)
  6. {
  7.     // do stuff
  8. }
  9. catch (IndexOutOfRangeException e)
  10. {
  11.     // do stuff
  12. }
  13. catch (Exception e)
  14. {
  15.     // do stuff
  16. }
You have to catch Exception last because it's a more general than the other two. This is actually based of inheritance. If you have Visual Studio, you can right-click on the exception type and select "Go To Definition" to bring up the metadata and see the class definition and follow the inheritance chain.

I hope that helps :)

4 2654
tlhintoq
3,525 Expert 2GB
There is no actual question in your post
Dec 6 '09 #2
GaryTexmo
1,501 Expert 1GB
Hmm, where to start. Ok, the first thing I'll say is you can't do what you're trying to do... I'll carry on with explaining why but sorry if it seems a bit disjointed.

To address your immediate question, you are getting that error message because you're using a type like a variable, just like the error message says. A switch statement is expecting a value, not a type. If you want that to work, you'd need to change it to...

Expand|Select|Wrap|Line Numbers
  1. switch (e.GetType())
  2. {
  3.     case typeof(FileNotFoundException):
  4.         // do stuff
  5.         break;
  6. }
Now, that still won't compile because a switch statement can only work on constant values of an integral type (things like int, byte, etc... strings count here too). So what you'd need to do is change that switch statement to an if-else-if-else statement, as such...

Expand|Select|Wrap|Line Numbers
  1. if (e.GetType() == typeof(FileNotFoundException))
  2. {
  3.     // do stuff
  4. }
  5. else if (e.GetType() == <exception type>)
  6. {
  7.     // do stuff
  8. }
  9. else
  10. {
  11.     // do stuff
  12. }
Now, you can do this right as a part of your try/catch block, so you don't really need to set this up yourself, you just catch the exception you want and handle it accordingly. The caveat here is that you have to organize them properly, you catch the specific exceptions first, then the generic exceptions. So, for example...

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.  
  4. }
  5. catch (FileNotFoundException e)
  6. {
  7.     // do stuff
  8. }
  9. catch (IndexOutOfRangeException e)
  10. {
  11.     // do stuff
  12. }
  13. catch (Exception e)
  14. {
  15.     // do stuff
  16. }
You have to catch Exception last because it's a more general than the other two. This is actually based of inheritance. If you have Visual Studio, you can right-click on the exception type and select "Go To Definition" to bring up the metadata and see the class definition and follow the inheritance chain.

I hope that helps :)
Dec 6 '09 #3
Samishii23
246 100+
There is no actual question in your post
What am I doing wrong?

If you have Visual Studio, you can right-click on the exception type and select "Go To Definition" to bring up the metadata and see the class definition and follow the inheritance chain.
The meta data for all the exceptions that can happen with the given input?

I was going to do the try-catch blocks at the processing point, and have a function called to log, and throw a message box to the user stating the issue, so maybe they can fix it on their end. Also in case of certain errors, I would have the function close the application.

Searching through the VS data.
Dec 6 '09 #4
GaryTexmo
1,501 Expert 1GB
The meta data for all the exceptions that can happen with the given input?
No, what I meant by that is if you want to see the inheritance path for an exception, you can follow it doing that.

You should be able to use the last method that I had there and do what you want. If there's anything you want to do at the end of your try/catch bock, regardless of the error, you can use finally. I'd recommend not closing your form right in the catch part though, perhaps set a flag so that it can close properly at a more appropriate time.
Dec 6 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
2
by: Randy Harris | last post by:
I thought that I had a grasp of how VBA error handling functioned, but have just become painfully aware that I don't. I thought that the "On Error GoTo 0" in the second sub below would turn off...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
16
by: Steve Jorgensen | last post by:
I'm trying to figure out if there is a way to generate standard error handlers that "know" if the calling code has an error handler in effect (On Error Goto <label> or On Error Resume Next) before...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
2
by: robert d via AccessMonster.com | last post by:
I have a module with an error handler section. At the top of the module is the following statement: On Error GoTo Err_Ctrl In the error handler is a conditional If statement based on an error...
10
by: robert d via AccessMonster.com | last post by:
I have a global error handler that up until today has been working flawlessly. Let me first provide the relevant code **************************************************************** On Error...
2
by: Jukka Aho | last post by:
When converting Unicode strings to legacy character encodings, it is possible to register a custom error handler that will catch and process all code points that do not have a direct equivalent in...
4
by: jdokos | last post by:
Hello All, I have a procedure that is getting -443 after upgrading to V9.5 FP1. The procedure was written to output only the SQLCODE. Here is the output that is returned: Value of output...
9
by: Daniel Smedegaard Buus | last post by:
Hey all :) I was wondering about the $error_types (I particularly notice the 's' suffix when reading the manual) parameter for 'set_error_handler()': Can be used to mask the triggering of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.