2

We often use try catch statements in our method, if the method can return a value, but the value doesn't a string, how to return the exception message? For example:

public int GetFile(string path) { int i; try { //... return i; } catch (Exception ex) { // How to return the ex? // If the return type is a custom class, how to deal with it? } } 

How to return the exception?

1
  • 1
    You cannot return the exception message here. Return some int value which is understood as an error code. Or, throw a custom exception which the calling functions/classes can handle. If return type is a custom class, you could send a null object, or create a new object with some values set which indicate an error has ocurred. Commented Dec 28, 2012 at 5:00

3 Answers 3

2

You can remove try catch block to throw exception or throw exception from catch block if you want to do something useful in catch block like logging the exception. If you want to send exception message from your method and do not want to throw exception then you can use out string variable to hold the exception message for calling method.

public int GetFile(string path, out string error) { error = string.Empty. int i; try { //... return i; } catch(Exception ex) { error = ex.Message; // How to return the ex? // If the return type is a custom class, how to deal with it? } } 

How to call the method.

string error = string.Empty; GetFile("yourpath", out error); 
Sign up to request clarification or add additional context in comments.

Comments

2

If you simply want to throw any exception, remove try/catch block.

If you want to handle specific exceptions you have two options

  1. Handle those exceptions only.

    try { //... return i; } catch(IOException iex) { // do something throw; } catch(PathTooLongException pex) { // do something throw; } 
  2. In generic handler do something for certain types

    try { //... return i; } catch(Exception ex) { if (ex is IOException) { // do something } if (ex is PathTooLongException) { // do something } throw; } 

6 Comments

No-no-no. You should not catch all exceptions and then check type. Catch target type explicitly, i.e. catch (IOException)
you are right. But sometimes that is a very big bolierplate code if to be applied multiple times. There i use generic exception handler.
You can extract shared code into a separate method to increase reuse. But yes, indeed, exception handling is often a sort of functional programming with duplicating code.
In code #2 a number of checks (or more complex logic) will run first and only then unmatched exception will be thrown. If you don't catch-them-all then it will be thrown immediately. Also such approach increases a chance of mistake.
Better to increase shared code and decrease duplicated of possible :)
|
0

you can directly throw your exception, and from calling method or event catch that exception.

public int GetFile(string path) { int i; try { //... return i; } catch (Exception ex) { throw ex; } } 

and catch that in calling method like this...

public void callGetFile() { try { int result = GetFile("your file path"); } catch(exception ex) { //Catch your thrown excetion here } } 

1 Comment

Don't throw ex. Use just throw instead, it doesn't erase the callstack then.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.