Skip to content

Commit 86152ec

Browse files
Update Exceptions Topic
1 parent d4e291c commit 86152ec

8 files changed

Lines changed: 107 additions & 8 deletions

File tree

docs/programming-languages/exceptions/_cpp.mdx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@ catch (...) // catch-all handler (Yes, ellipses)
2828
// Custom Exception
2929
// ------------------------------------
3030

31-
// TODO
31+
class MyException : public std::exception
32+
{
33+
public:
34+
const char* what() const noexcept override
35+
{
36+
return "Something went wrong";
37+
}
38+
};
39+
40+
throw MyException();
3241
```
3342
> More Info:
3443
> - https://www.learncpp.com/cpp-tutorial/144-uncaught-exceptions-catch-all-handlers-and-exception-specifiers/

docs/programming-languages/exceptions/_csharp.mdx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,22 @@ finally // Optional
3131
// Custom Exception
3232
// ------------------------------------
3333
34-
// TODO
34+
public class MyException : Exception
35+
{
36+
public MyException()
37+
{
38+
}
39+
40+
public MyException(string message) : base(message)
41+
{
42+
}
43+
44+
public MyException(string message, Exception innerException) : base(message, innerException)
45+
{
46+
}
47+
}
48+
49+
throw new MyException("Something went wrong");
3550
```
3651

3752
> More Info:
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
```Go
2-
// TODO
2+
// Go does not use exceptions.
3+
// Return errors as values.
4+
5+
func divide(first int, second int) (int, error) {
6+
if second == 0 {
7+
return 0, errors.New("division by zero")
8+
}
9+
10+
return first / second, nil
11+
}
12+
13+
value, err := divide(10, 2)
14+
if err != nil {
15+
return err
16+
}
17+
18+
// panic/recover exists, but is usually reserved for unrecoverable errors.
319
```
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
11
```Java
2-
// TODO
2+
// ------------------------------------
3+
// Handling Exceptions
4+
// ------------------------------------
5+
try {
6+
// ...
7+
}
8+
catch (YourSpecificException error) {
9+
// ...
10+
}
11+
catch (Exception error) {
12+
// ...
13+
}
14+
finally {
15+
// This block will run for success or catch
16+
}
17+
18+
// ------------------------------------
19+
// Custom Exception
20+
// ------------------------------------
21+
class MyException extends Exception {
22+
public MyException(String message) {
23+
super(message);
24+
}
25+
}
26+
27+
throw new MyException("Something went wrong");
328
```

docs/programming-languages/exceptions/_javascript.mdx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ finally {
3232
// Custom Exception
3333
// ------------------------------------
3434

35-
// TODO
35+
class MyError extends Error {
36+
constructor(message) {
37+
super(message);
38+
this.name = "MyError";
39+
}
40+
}
41+
42+
throw new MyError("Something went wrong");
3643
```
3744

3845
> More Info:

docs/programming-languages/exceptions/_python.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ def divide(x, y):
4141
# Custom Exception
4242
# ------------------------------------
4343

44-
# TODO
44+
class MyError(Exception):
45+
pass
46+
47+
raise MyError("Something went wrong")
4548
```
4649

4750
> More Info:
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
```Rust
2-
// TODO
2+
// Rust does not use exceptions for recoverable errors.
3+
// Use Result<T, E>.
4+
5+
fn divide(first: i32, second: i32) -> Result<i32, String> {
6+
if second == 0 {
7+
return Err(String::from("division by zero"));
8+
}
9+
10+
Ok(first / second)
11+
}
12+
13+
match divide(10, 2) {
14+
Ok(value) => println!("{}", value),
15+
Err(error) => println!("{}", error),
16+
}
17+
18+
// Panic is available for unrecoverable errors.
19+
panic!("Something went wrong");
320
```

docs/programming-languages/exceptions/_typescript.mdx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ finally {
3333
// Custom Exception
3434
// ------------------------------------
3535

36-
// TODO
36+
class MyError extends Error {
37+
public constructor(message: string) {
38+
super(message);
39+
this.name = "MyError";
40+
}
41+
}
42+
43+
throw new MyError("Something went wrong");
3744
```
3845
> More Info:
3946
> - https://github.com/Microsoft/TypeScript/issues/20024

0 commit comments

Comments
 (0)