Created
February 20, 2015 09:00
-
-
Save rschumm/143e1d8d1f5b221b61aa to your computer and use it in GitHub Desktop.
Exceptions
This file contains hidden or 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 SafeDivision(int x, int y) | |
{ | |
try | |
{ | |
return (x / y); | |
} | |
catch (System.DivideByZeroException dbz) | |
{ | |
System.Console.WriteLine("Division by zero attempted!"); | |
return 0; | |
} | |
} |
This file contains hidden or 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 { | |
room.openDoor(); | |
room.enter(person); | |
} catch(e) { | |
// ... | |
} finally { | |
room.reset(); | |
} |
This file contains hidden or 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
function myFunction() { | |
var message, x; | |
message = document.getElementById("message"); | |
message.innerHTML = ""; | |
x = document.getElementById("demo").value; | |
try { | |
x = Number(x); | |
if(x == "") throw "is empty"; | |
if(isNaN(x)) throw "is not a number"; | |
if(x > 10) throw "is too high"; | |
if(x < 5) throw "is too low"; | |
} | |
catch(err) { | |
message.innerHTML = "Error: " + err + "."; | |
} | |
finally { | |
document.getElementById("demo").value = ""; | |
} | |
} |
This file contains hidden or 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{ | |
throwsException(); | |
println("this line is never executed"); | |
} catch { | |
case e: Exception => println("exception caught: " + e); | |
} | |
//A METHOD THAT THROWS EXCEPTION | |
def throwsException() { | |
throw new IllegalStateException("Exception thrown"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment