Jump to content

Exceptions: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 410:
'''error'''
<lang applescript>error "Error message." number 2000</lang>
 
=={{header|AutoHotkey}}==
=== True exceptions ===
Line 633 ⟶ 634:
</lang>
Now all we need to do is add a finally block :) hint: it is possible
 
=={{header|C sharp|C#}}==
{{works with|Visual Studio|2005}}
 
'''Defining exceptions'''
<lang csharp>public class MyException : Exception
{
// data with info about exception
};</lang>
 
'''Throw exceptions'''
<lang csharp>void foo()
{
throw MyException();
}</lang>
 
'''Catching exceptions'''
<lang csharp>try {
foo();
}
catch (MyException e)
{
// handle exceptions of type MyException and derived
}
catch
{
// handle any type of exception not handled by above catches
}</lang>
 
=={{header|C++}}==
Line 691 ⟶ 720:
}
catch (...)
{
// handle any type of exception not handled by above catches
}</lang>
 
=={{header|C sharp|C#}}==
{{works with|Visual Studio|2005}}
 
'''Defining exceptions'''
<lang csharp>public class MyException : Exception
{
// data with info about exception
};</lang>
 
'''Throw exceptions'''
<lang csharp>void foo()
{
throw MyException();
}</lang>
 
'''Catching exceptions'''
<lang csharp>try {
foo();
}
catch (MyException e)
{
// handle exceptions of type MyException and derived
}
catch
{
// handle any type of exception not handled by above catches
Line 839 ⟶ 840:
end;
end;</lang>
 
=={{header|Déjà Vu}}==
 
<lang dejavu>stuff-going-wrong:
raise :value-error
 
try:
stuff-going-wrong
catch value-error:
!print "Whoops!"</lang>
{{out}}
<pre>Whoops!</pre>
 
=={{header|DWScript}}==
Line 882 ⟶ 871:
end;
end;</lang>
 
=={{header|Déjà Vu}}==
 
<lang dejavu>stuff-going-wrong:
raise :value-error
 
try:
stuff-going-wrong
catch value-error:
!print "Whoops!"</lang>
{{out}}
<pre>Whoops!</pre>
 
=={{header|E}}==
Line 1,442 ⟶ 1,443:
eventSetup = true;
}</lang>
 
=={{header|jq}}==
{{works with|jq|>1.4}}
Line 1,713 ⟶ 1,715:
print("errmsg = ", errmsg)
</lang>
 
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
Line 1,825 ⟶ 1,828:
CleanupFoo(); (* always executed *)
END;</lang>
 
 
=={{header|MOO}}==
Line 1,994 ⟶ 1,996:
| Another_Exception s -> s
| _ -> "unknown exception"</lang>
 
 
=={{header|Oforth}}==
Line 2,234 ⟶ 2,235:
'''Other styles'''<br>
More complicated exception handling can be achieved in Perl using [http://search.cpan.org/perldoc?TryCatch TryCatch] or [http://search.cpan.org/perldoc?Exception::Class Exception::Class] modules.
 
=={{header|Perl 6}}==
{{works with|rakudo|2015-09-10}}
The Perl 6 equivalent to Perl 5's eval {...} is try {...}. A try block by default has a CATCH block that handles all fatal exceptions by ignoring them. If you define a CATCH block within the try, it replaces the default CATCH. It also makes the try keyword redundant, because any block can function as a try block if you put a CATCH block within it. The inside of a CATCH functions as a switch statement on the current exception.
<lang perl6>try {
die "Help I'm dieing!";
CATCH {
when X::AdHoc { note .Str.uc; say "Cough, Cough, Aiee!!" }
default { note "Unexpected exception, $_!" }
}
}
 
say "Yay. I'm alive.";
 
die "I'm dead.";
 
say "Arrgh.";
 
CATCH {
default { note "No you're not."; say $_.Str; }
}</lang>
 
<pre>
HELP I'M DIEING!
Cough, Cough, Aiee!!
Yay. I'm alive.
No you're not.
I'm dead.
</pre>
 
Perl 6 comes with [http://design.perl6.org/S04.html#Phasers phasers], that are called when certain conditions in the life of a program, routine or block are met. <tt>CATCH</tt> is one of them and works nicely together with <tt>LEAVE</tt> that is called even if an exception would force the current block to be left immediately. It's a nice place to put your cleanup code.
 
<lang perl6>sub f(){
ENTER { note '1) f has been entered' }
LEAVE { note '2) f has been left' }
say '3) here be dragons';
die '4) that happend to be deadly';
}
 
f();
say '5) am I alive?';
 
CATCH {
when X::AdHoc { note q{6) no, I'm dead}; }
}</lang>
 
<pre>1) f has been entered
3) here be dragons
6) no, I'm dead
2) f has been left</pre>
 
=={{header|Phix}}==
Line 2,688 ⟶ 2,639:
(raise (exn:my-exception "Hi!" (current-continuation-marks))))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-09-10}}
The Perl 6 equivalent to Perl 5's eval {...} is try {...}. A try block by default has a CATCH block that handles all fatal exceptions by ignoring them. If you define a CATCH block within the try, it replaces the default CATCH. It also makes the try keyword redundant, because any block can function as a try block if you put a CATCH block within it. The inside of a CATCH functions as a switch statement on the current exception.
<lang perl6>try {
die "Help I'm dieing!";
CATCH {
when X::AdHoc { note .Str.uc; say "Cough, Cough, Aiee!!" }
default { note "Unexpected exception, $_!" }
}
}
 
say "Yay. I'm alive.";
 
die "I'm dead.";
 
say "Arrgh.";
 
CATCH {
default { note "No you're not."; say $_.Str; }
}</lang>
 
<pre>
HELP I'M DIEING!
Cough, Cough, Aiee!!
Yay. I'm alive.
No you're not.
I'm dead.
</pre>
 
Perl 6 comes with [http://design.perl6.org/S04.html#Phasers phasers], that are called when certain conditions in the life of a program, routine or block are met. <tt>CATCH</tt> is one of them and works nicely together with <tt>LEAVE</tt> that is called even if an exception would force the current block to be left immediately. It's a nice place to put your cleanup code.
 
<lang perl6>sub f(){
ENTER { note '1) f has been entered' }
LEAVE { note '2) f has been left' }
say '3) here be dragons';
die '4) that happend to be deadly';
}
 
f();
say '5) am I alive?';
 
CATCH {
when X::AdHoc { note q{6) no, I'm dead}; }
}</lang>
 
<pre>1) f has been entered
3) here be dragons
6) no, I'm dead
2) f has been left</pre>
 
=={{header|Raven}}==
Line 3,288 ⟶ 3,290:
In this example, the exception handler appends some additional
verbiage to the message.
 
=={{header|V}}==
 
Line 3,300 ⟶ 3,303:
<lang v>[myproc] [puts] catch
=[new error 1 2 3]</lang>
 
=={{header|Visual Basic .NET}}==
 
'''Defining exceptions'''
<lang vbnet>Class MyException
Inherits Exception
'data with info about exception
End Class</lang>
 
'''Throw exceptions'''
<lang vbnet>Sub foo()
Throw New MyException
End Sub</lang>
 
'''Catching exceptions'''
<lang vbnet>Sub bar()
Try
foo()
Catch e As MyException When e.Data.Contains("Foo")
' handle exceptions of type MyException when the exception contains specific data
Catch e As MyException
' handle exceptions of type MyException and derived exceptions
Catch e As Exception
' handle any type of exception not handled by above catches
Finally
'code here occurs whether or not there was an exception
End Try
End Sub</lang>
 
=={{header|VBA}}==
Line 3,392 ⟶ 3,366:
end_try:
'by convention, often just a drop through from the catch block
End Sub</lang>
 
=={{header|Visual Basic .NET}}==
 
'''Defining exceptions'''
<lang vbnet>Class MyException
Inherits Exception
'data with info about exception
End Class</lang>
 
'''Throw exceptions'''
<lang vbnet>Sub foo()
Throw New MyException
End Sub</lang>
 
'''Catching exceptions'''
<lang vbnet>Sub bar()
Try
foo()
Catch e As MyException When e.Data.Contains("Foo")
' handle exceptions of type MyException when the exception contains specific data
Catch e As MyException
' handle exceptions of type MyException and derived exceptions
Catch e As Exception
' handle any type of exception not handled by above catches
Finally
'code here occurs whether or not there was an exception
End Try
End Sub</lang>
 
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.