View WithdrawThrow2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void WithdrawMoney(decimal amount) | |
{ | |
if (amount <= 0) | |
{ | |
throw new ArgumentOutOfRangeException(nameof(amount), amount, "The amount to withdraw must be positive"); | |
} | |
// Actual withdraw code | |
} |
View WithdrawThrow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void WithdrawMoney(decimal amount) | |
{ | |
if (amount <= 0) | |
{ | |
throw new InvalidOperationException(); | |
} | |
// Actual withdraw code | |
} |
View Throw.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
// Do something that might cause an exception | |
} | |
catch (IOException ex) | |
{ | |
// Rethrow the exception without replacing its call stack | |
throw; | |
} |
View ThrowEx1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
// Do something that might cause an exception | |
} | |
catch (IOException ex) | |
{ | |
// We decide we don't really want to handle this exception | |
// Let's re-throw it so calling methods can catch it | |
throw ex; | |
} |
View ThrowEx.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (quantity <= 0) | |
{ | |
throw new Exception("Quantity must be a positive number"); | |
} |
View CatchEx2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
// Some code | |
} | |
catch | |
{ | |
} |
View CatchEx1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
// Some code | |
} | |
catch (Exception ex) | |
{ | |
} |
View CatchIOOR.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int sum = 0; | |
try | |
{ | |
for (int i = 0; i <= myArray.Length; i++) | |
{ | |
sum += myArray[i]; | |
} | |
} | |
catch (IndexOutOfRangeException ex) | |
{ |
View FindLargest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int FindLargestNumber(List<int> numbers) | |
{ | |
int largest = int.MinValue; | |
if (numbers != null) | |
{ | |
foreach (int num in numbers) | |
{ | |
if (largest < num) | |
{ | |
largest = num; |
View IOEx2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
string text = File.ReadAllText("MyFile.txt"); | |
// Do something with text | |
} | |
catch (IOException ex) | |
{ | |
// Display an error message | |
} |
NewerOlder