Exceptions: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 85: Line 85:
}
}


==[Java]==
===Defining exceptions===
//Checked exception
public class MyException extends Exception {
//Put specific info in here
}

//Unchecked exception
public class MyRuntimeException extends RuntimeException {}

===Throw exceptions===
public void fooChecked() throws MyException {
throw new MyException();
}

public void fooUnchecked() {
throw new MyRuntimeException();
}

===Catching exceptions===
try {
fooChecked();
}
catch(MyException exc) {
//Catch only your specified type of exception
}
catch(Exception exc) {
//Catch any non-system error exception
}
catch(Throwable exc) {
//Catch everything including system errors (not recommended)
}
finally {
//This code is always executed after exiting the try block
}
==[[Python]]==
==[[Python]]==
[[Category:Python]]
[[Category:Python]]