Skip to content

Instantly share code, notes, and snippets.

@rschumm
Created February 20, 2015 09:00
Show Gist options
  • Save rschumm/143e1d8d1f5b221b61aa to your computer and use it in GitHub Desktop.
Save rschumm/143e1d8d1f5b221b61aa to your computer and use it in GitHub Desktop.
Exceptions
int SafeDivision(int x, int y)
{
try
{
return (x / y);
}
catch (System.DivideByZeroException dbz)
{
System.Console.WriteLine("Division by zero attempted!");
return 0;
}
}
try {
room.openDoor();
room.enter(person);
} catch(e) {
// ...
} finally {
room.reset();
}
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 = "";
}
}
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