Jump to content

Exceptions: Difference between revisions

no edit summary
No edit summary
No edit summary
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]]==
[[Category:Python]]
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.