Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek

Similar presentations


Presentation on theme: "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek"— Presentation transcript:

1 CHARLES UNIVERSITY IN PRAGUE http://d3s.mff.cuni.cz/~jezek faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek pavel.jezek@d3s.mff.cuni.cz Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License (http://www.msdnaa.net/curriculum/license_curriculum.aspx)

2 Implicitly Typed Local Variables Examples: var i = 5; var s = "Hello"; var d = 1.0; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary (); Are equivalent to: int i = 5; string s = "Hello"; double d = 1.0; int[] numbers = new int[] {1, 2, 3}; Dictionary orders = new Dictionary (); Errors: var x; // Error, no initializer to infer type from var y = {1, 2, 3}; // Error, collection initializer not permitted var z = null; // Error, null type not permitted

3 Exception Hierarchy (excerpt) Exception SystemException ArithmeticException DivideByZeroException OverflowException... NullReferenceException IndexOutOfRangeException InvalidCastException... ApplicationException... user-defined exceptions... IOException FileNotFoundException DirectoryNotFoundException... WebException...

4 try Statement FileStream s = null; try { s = new FileStream(curName, FileMode.Open);... } catch (FileNotFoundException e) { Console.WriteLine("file {0} not found", e.FileName); } catch (IOException) { Console.WriteLine("some IO exception occurred"); } catch { Console.WriteLine("some unknown error occurred"); } finally { if (s != null) s.Close(); } catch clauses are checked in sequential order. finally clause is always executed (if present). Exception parameter name can be omitted in a catch clause. Exception type must be derived from System.Exception. If exception parameter is missing, System.Exception is assumed.

5 System.Exception Properties e.Messagethe error message as a string; set by new Exception(msg); e.StackTracetrace of the method call stack as a string e.Sourcethe application or object that threw the exception e.TargetSitethe method object that threw the exception... e.InnerExceptionshould be always set if rethrowing another exception Methods e.ToString() returns the name of the exception and the StackTrace...


Download ppt "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek"

Similar presentations


Ads by Google