PDA

View Full Version : [C#]Restituzione valore in caso eccezione


RaouL_BennetH
10-07-2009, 16:47
Ciao a tutti :)

Stavo scrivendo questa piccola classe:


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DictionaryResource
{

public class DictionaryLoader
{

public static IDictionary<string, string> UserList(string fileName)
{
IDictionary<string, string> uList = new Dictionary<string, string>();
try
{
StreamReader sr = new StreamReader(fileName);
string valore ;
while((valore = sr.ReadLine()) != null)
{
string[] col = valore.Split(',');
uList.Add(col[0], col[1]);
}
sr.Close();
return oList;
}
//qui il mio dubbio:
catch(IOException ex)
{
//se catturo 'ex', cosa devo far restituire?
}
catch(Exception ex)
{
//stesso discorso
}
}


Inizialmente avevo pensato semplicemente:



throw ex;



Altrimenti dovrei far tornare un dizionario vuoto :mbe:


Grazie a tutti :)

RaouL.

CwNd
11-07-2009, 09:53
Secondo me non hai tante alternative: se devi mettere un


throw ex;


tantovale che non gestisci neanche l'eccezione perchč nel caso non č gestita viene automaticamente lanciata al chiamante, ed otterresti lo stesso effetto e in modo pių performante (i try-catch sono abbastanza pesanti).

Se invece vuoi gestire le eccezioni, secondo me un possibile valore di ritorno potrebbe essere il null.

Ciao