New scenario YouTubeexception etc

This commit is contained in:
2019-01-09 23:23:56 +01:00
parent f455ac39ff
commit 5f7139c481
5 changed files with 62 additions and 9 deletions

View File

@ -46,6 +46,9 @@
<Compile Include="Calculator.cs" /> <Compile Include="Calculator.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Video.cs" />
<Compile Include="YouTubeApi.cs" />
<Compile Include="YouTubeException.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -12,21 +11,18 @@ namespace Exceptionhandling
static void Main(string[] args) static void Main(string[] args)
{ {
StreamReader streamreader = null;
try try
{ {
streamreader = new StreamReader(@"c:\file.zip"); using(var streamreader = new StreamReader(@"c:\file.zip"))
{
var content = streamreader.ReadToEnd(); var content = streamreader.ReadToEnd();
} }
}
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("Sorry, an unexpected error occured. :" + ex.Message); Console.WriteLine("Sorry, an unexpected error occured. :" + ex.Message);
} }
finally
{
if (streamreader != null)
streamreader.Dispose();
}
} }
} }
} }

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exceptionhandling
{
public class Video
{
public string Title { get; set; }
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
namespace Exceptionhandling
{
public class YouTubeApi
{
public List<Video> GetVideos(string user)
{
try
{
// Access YouTube web interface
// Read the data
// Create a list of Video objects
throw new Exception("Ooops some low level YouTube error.");
}
catch (Exception ex)
{
//Log
throw new YouTubeException("Could not fetch the videos from YouTube.",ex);
}
return new List<Video>();
}
}
}

View File

@ -0,0 +1,13 @@
using System;
namespace Exceptionhandling
{
public class YouTubeException : Exception
{
public YouTubeException(string message, Exception innerException)
: base(message, innerException)
{
}
}
}