Is there a language that let's you use a try block without a catch block?Avoiding new operator in JavaScript — the better waySynchronous vs. asynchronous for publish subscribe communication between JavaScript objectsAdvantages of using pure JavaScript over JQueryClient side authentication through signatures instead of passwordsText conversion - Javascript before saving to db, or php when retrieving?Unit Testing a stateful framework such as Phaser?Using a try/catch to deal with deep object drill downs that will often failIs there a way to use html5 custom elements without javascript?An options parameter vs chained functions for object initialization?Difference in use-cases for just using '.catch' v.s using 'Throw Error…' with '.catch'
Can an airline pilot be prosecuted for killing an unruly passenger who could not be physically restrained?
What would be the game balance implications for using the Gygax method for applying falling damage?
Will this series of events work to drown the Tarrasque?
Shortest amud or daf in Shas?
Why is Drogon so much better in battle than Rhaegal and Viserion?
Save my secrets!
I recently started my machine learning PhD and I have absolutely no idea what I'm doing
Is my company merging branches wrong?
Should I twist DC power and ground wires from a power supply?
How to say "that" as in "the cow that ate" in Japanese?
In Dutch history two people are referred to as "William III"; are there any more cases where this happens?
Can a generation ship withstand its own oxygen and daily wear for many thousands of years?
Show that the characteristic polynomial is the same as the minimal polynomial
Who is frowning in the sentence "Daisy looked at Tom frowning"?
Should all adjustments be random effects in a mixed linear effect?
Physically unpleasant work environment
Have the writers and actors of GOT responded to its poor reception?
Working hours and productivity expectations for game artists and programmers
Does the US Supreme Court vote using secret ballots?
Why use a retrograde orbit?
Divisor Rich and Poor Numbers
How do you cope with rejection?
FIFO data structure in pure C
Can the Gate spell draw a creature larger that 20 feet in every dimension through the portal it creates?
Is there a language that let's you use a try block without a catch block?
Avoiding new operator in JavaScript — the better waySynchronous vs. asynchronous for publish subscribe communication between JavaScript objectsAdvantages of using pure JavaScript over JQueryClient side authentication through signatures instead of passwordsText conversion - Javascript before saving to db, or php when retrieving?Unit Testing a stateful framework such as Phaser?Using a try/catch to deal with deep object drill downs that will often failIs there a way to use html5 custom elements without javascript?An options parameter vs chained functions for object initialization?Difference in use-cases for just using '.catch' v.s using 'Throw Error…' with '.catch'
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Is there any language that let's you use a try block without a catch block?
The complier or parser complains with this code:
try
const utils = require("applicationutils");
But it is ok with this code:
try
const utils = require("applicationutils");
catch(e)
I don't need the catch block.
I'm using JavaScript if it matters.
Update Example Code:
// setting defaults ahead of try - no need for a catch block
var setting = 10;
var myRegEx = "/123/g";
var supportsRegEx2 = false;
try
const utils = require("applicationutils");
setting = 20;
myRegEx = "/123/gm";
supportsRegEx2 = true;
javascript try catch
add a comment |
Is there any language that let's you use a try block without a catch block?
The complier or parser complains with this code:
try
const utils = require("applicationutils");
But it is ok with this code:
try
const utils = require("applicationutils");
catch(e)
I don't need the catch block.
I'm using JavaScript if it matters.
Update Example Code:
// setting defaults ahead of try - no need for a catch block
var setting = 10;
var myRegEx = "/123/g";
var supportsRegEx2 = false;
try
const utils = require("applicationutils");
setting = 20;
myRegEx = "/123/gm";
supportsRegEx2 = true;
javascript try catch
Could you explain why you want a try without a catch-block?
– Pieter B
4 mins ago
add a comment |
Is there any language that let's you use a try block without a catch block?
The complier or parser complains with this code:
try
const utils = require("applicationutils");
But it is ok with this code:
try
const utils = require("applicationutils");
catch(e)
I don't need the catch block.
I'm using JavaScript if it matters.
Update Example Code:
// setting defaults ahead of try - no need for a catch block
var setting = 10;
var myRegEx = "/123/g";
var supportsRegEx2 = false;
try
const utils = require("applicationutils");
setting = 20;
myRegEx = "/123/gm";
supportsRegEx2 = true;
javascript try catch
Is there any language that let's you use a try block without a catch block?
The complier or parser complains with this code:
try
const utils = require("applicationutils");
But it is ok with this code:
try
const utils = require("applicationutils");
catch(e)
I don't need the catch block.
I'm using JavaScript if it matters.
Update Example Code:
// setting defaults ahead of try - no need for a catch block
var setting = 10;
var myRegEx = "/123/g";
var supportsRegEx2 = false;
try
const utils = require("applicationutils");
setting = 20;
myRegEx = "/123/gm";
supportsRegEx2 = true;
javascript try catch
javascript try catch
edited 3 hours ago
1.21 gigawatts
asked 4 hours ago
1.21 gigawatts1.21 gigawatts
549414
549414
Could you explain why you want a try without a catch-block?
– Pieter B
4 mins ago
add a comment |
Could you explain why you want a try without a catch-block?
– Pieter B
4 mins ago
Could you explain why you want a try without a catch-block?
– Pieter B
4 mins ago
Could you explain why you want a try without a catch-block?
– Pieter B
4 mins ago
add a comment |
3 Answers
3
active
oldest
votes
Many languages permit: try ... finally ...
or some variant. Take C# as an example. There are plenty of others.
But there is no point in a try ...
. It has no meaning without an associated:
catch(...) ...
finally ...
catch(...) ... finally ...
.catch(...) ... catch(...) ... ... catch(...) ...
.
The use case you are highlighting is actually very bad code.
If you are expecting an error, and you do nothing about it, your design is fundamentally flawed.
You could say that this syntax is working as expected. Because it is irritating you enough to look for a solution.
The solution is to either get rid of the try
altogether, or figure out how to handle the error.
"There is no point to a try ". Hmm... "There's never a use case for it and so it's bad code?" You don't know enough about the situation to know that. Should every if statement have an else statement? The compiler throws errors when you encounter a class you want to import that doesn't exist but you have to import it to use it. I'll add more code.
– 1.21 gigawatts
3 hours ago
1
try finally
won't suppress the exception though, it'll get raised in the caller. It seems the OP wants to suppress all exceptions.
– whatsisname
3 hours ago
2
@1.21gigawatts: Exactly what behavior do you want, then? There are only two things that I could imagine happening in this case. The exception gets caught, consumed and dropped; or the exception passes through your code and out into the caller like normal.
– Nicol Bolas
3 hours ago
add a comment |
I don't need the catch block.
But you do need to catch. The behavior of your code with a catch block is to catch any exception, and then forget that it happened. So any exception that tries to pass through will stop, and your code will basically pretend that the try
block executed successfully.
So you want a naked try
block to act like it catches an exception and ignores it. Here's the thing: a default case is meant to be a common case, one that is useful by many users and not error prone. if
doesn't typically require an else
because there are many cases where you have nothing to do.
I know nothing about why you want to drop exceptions on the floor and pretend they didn't happen. I'm willing to accept that you have some good justification for doing so. But the fact is, in the general case, it's not a good idea. Most programmers don't want to do it, and there are good arguments to say that it is generally unwise to do this sort of thing.
A good language will let you do something unwise. But a good language will not let you do something unwise by accident. Since the behavior you want is generally unwise, languages tend to make you explicitly request it.
FWIW,if
does require anelse
in Haskell since it's not possible to "do nothing".
– immibis
1 hour ago
add a comment |
What would that mean?
I would expect:
try
const utils = require("applicationutils");
To mean the same thing as:
const utils = require("applicationutils");
try/catch/finally is a well understood pattern, used in a lot of different languages. Exceptions by their very nature imply that there is a recovery handler somewhere else.
It sounds like what you are looking for is VB’s On Error Resume Next, which catches and ignores errors on a line by line basis. Today this is generally considered a bad practice, because it means that there is no actual error handling going on — flow continues as if the previous line had succeeded, which can lead to escalating the level of garbage/damage that is done by bad input, instead of halting or eliminating just the bad input.
The try/catch pattern is useful, because it can segment your algorithm into parts where you can fail but recover, and parts where there is no recovery after failure. And in the event that there is no recovery after failure, it is that part that cannot continue not the rest of the application.
If you have a need for this in js, you could get the same effect, by creating a function that takes a function as it’s argument, wraps it in a executes it inside a try catch block where it just sets an error object. You could then use lambda expressions for any line you would like to resume next on.
var err =
var ex = function(action)
try
action()
catch (e)
err=e
var i =5
ex(t=> alert(i.foo()))
console.log(i)
console.log(err.message)
Although it won’t work with lines that declare and initialize variables.
Look at the example on the question. OP does not wantsetting
,myRegEx
andsupportsRegEx2
to be modified ifrequire
fails. It is not a simpleOn Error Resume Next
. It would be more likeOn Error Goto Label
. Addendum: ultimately what OP wants is to omitcatch(e)
but have it behave exactly as if it were there.
– Theraot
11 mins ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "131"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f391999%2fis-there-a-language-that-lets-you-use-a-try-block-without-a-catch-block%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Many languages permit: try ... finally ...
or some variant. Take C# as an example. There are plenty of others.
But there is no point in a try ...
. It has no meaning without an associated:
catch(...) ...
finally ...
catch(...) ... finally ...
.catch(...) ... catch(...) ... ... catch(...) ...
.
The use case you are highlighting is actually very bad code.
If you are expecting an error, and you do nothing about it, your design is fundamentally flawed.
You could say that this syntax is working as expected. Because it is irritating you enough to look for a solution.
The solution is to either get rid of the try
altogether, or figure out how to handle the error.
"There is no point to a try ". Hmm... "There's never a use case for it and so it's bad code?" You don't know enough about the situation to know that. Should every if statement have an else statement? The compiler throws errors when you encounter a class you want to import that doesn't exist but you have to import it to use it. I'll add more code.
– 1.21 gigawatts
3 hours ago
1
try finally
won't suppress the exception though, it'll get raised in the caller. It seems the OP wants to suppress all exceptions.
– whatsisname
3 hours ago
2
@1.21gigawatts: Exactly what behavior do you want, then? There are only two things that I could imagine happening in this case. The exception gets caught, consumed and dropped; or the exception passes through your code and out into the caller like normal.
– Nicol Bolas
3 hours ago
add a comment |
Many languages permit: try ... finally ...
or some variant. Take C# as an example. There are plenty of others.
But there is no point in a try ...
. It has no meaning without an associated:
catch(...) ...
finally ...
catch(...) ... finally ...
.catch(...) ... catch(...) ... ... catch(...) ...
.
The use case you are highlighting is actually very bad code.
If you are expecting an error, and you do nothing about it, your design is fundamentally flawed.
You could say that this syntax is working as expected. Because it is irritating you enough to look for a solution.
The solution is to either get rid of the try
altogether, or figure out how to handle the error.
"There is no point to a try ". Hmm... "There's never a use case for it and so it's bad code?" You don't know enough about the situation to know that. Should every if statement have an else statement? The compiler throws errors when you encounter a class you want to import that doesn't exist but you have to import it to use it. I'll add more code.
– 1.21 gigawatts
3 hours ago
1
try finally
won't suppress the exception though, it'll get raised in the caller. It seems the OP wants to suppress all exceptions.
– whatsisname
3 hours ago
2
@1.21gigawatts: Exactly what behavior do you want, then? There are only two things that I could imagine happening in this case. The exception gets caught, consumed and dropped; or the exception passes through your code and out into the caller like normal.
– Nicol Bolas
3 hours ago
add a comment |
Many languages permit: try ... finally ...
or some variant. Take C# as an example. There are plenty of others.
But there is no point in a try ...
. It has no meaning without an associated:
catch(...) ...
finally ...
catch(...) ... finally ...
.catch(...) ... catch(...) ... ... catch(...) ...
.
The use case you are highlighting is actually very bad code.
If you are expecting an error, and you do nothing about it, your design is fundamentally flawed.
You could say that this syntax is working as expected. Because it is irritating you enough to look for a solution.
The solution is to either get rid of the try
altogether, or figure out how to handle the error.
Many languages permit: try ... finally ...
or some variant. Take C# as an example. There are plenty of others.
But there is no point in a try ...
. It has no meaning without an associated:
catch(...) ...
finally ...
catch(...) ... finally ...
.catch(...) ... catch(...) ... ... catch(...) ...
.
The use case you are highlighting is actually very bad code.
If you are expecting an error, and you do nothing about it, your design is fundamentally flawed.
You could say that this syntax is working as expected. Because it is irritating you enough to look for a solution.
The solution is to either get rid of the try
altogether, or figure out how to handle the error.
answered 4 hours ago
Kain0_0Kain0_0
4,892420
4,892420
"There is no point to a try ". Hmm... "There's never a use case for it and so it's bad code?" You don't know enough about the situation to know that. Should every if statement have an else statement? The compiler throws errors when you encounter a class you want to import that doesn't exist but you have to import it to use it. I'll add more code.
– 1.21 gigawatts
3 hours ago
1
try finally
won't suppress the exception though, it'll get raised in the caller. It seems the OP wants to suppress all exceptions.
– whatsisname
3 hours ago
2
@1.21gigawatts: Exactly what behavior do you want, then? There are only two things that I could imagine happening in this case. The exception gets caught, consumed and dropped; or the exception passes through your code and out into the caller like normal.
– Nicol Bolas
3 hours ago
add a comment |
"There is no point to a try ". Hmm... "There's never a use case for it and so it's bad code?" You don't know enough about the situation to know that. Should every if statement have an else statement? The compiler throws errors when you encounter a class you want to import that doesn't exist but you have to import it to use it. I'll add more code.
– 1.21 gigawatts
3 hours ago
1
try finally
won't suppress the exception though, it'll get raised in the caller. It seems the OP wants to suppress all exceptions.
– whatsisname
3 hours ago
2
@1.21gigawatts: Exactly what behavior do you want, then? There are only two things that I could imagine happening in this case. The exception gets caught, consumed and dropped; or the exception passes through your code and out into the caller like normal.
– Nicol Bolas
3 hours ago
"There is no point to a try ". Hmm... "There's never a use case for it and so it's bad code?" You don't know enough about the situation to know that. Should every if statement have an else statement? The compiler throws errors when you encounter a class you want to import that doesn't exist but you have to import it to use it. I'll add more code.
– 1.21 gigawatts
3 hours ago
"There is no point to a try ". Hmm... "There's never a use case for it and so it's bad code?" You don't know enough about the situation to know that. Should every if statement have an else statement? The compiler throws errors when you encounter a class you want to import that doesn't exist but you have to import it to use it. I'll add more code.
– 1.21 gigawatts
3 hours ago
1
1
try finally
won't suppress the exception though, it'll get raised in the caller. It seems the OP wants to suppress all exceptions.– whatsisname
3 hours ago
try finally
won't suppress the exception though, it'll get raised in the caller. It seems the OP wants to suppress all exceptions.– whatsisname
3 hours ago
2
2
@1.21gigawatts: Exactly what behavior do you want, then? There are only two things that I could imagine happening in this case. The exception gets caught, consumed and dropped; or the exception passes through your code and out into the caller like normal.
– Nicol Bolas
3 hours ago
@1.21gigawatts: Exactly what behavior do you want, then? There are only two things that I could imagine happening in this case. The exception gets caught, consumed and dropped; or the exception passes through your code and out into the caller like normal.
– Nicol Bolas
3 hours ago
add a comment |
I don't need the catch block.
But you do need to catch. The behavior of your code with a catch block is to catch any exception, and then forget that it happened. So any exception that tries to pass through will stop, and your code will basically pretend that the try
block executed successfully.
So you want a naked try
block to act like it catches an exception and ignores it. Here's the thing: a default case is meant to be a common case, one that is useful by many users and not error prone. if
doesn't typically require an else
because there are many cases where you have nothing to do.
I know nothing about why you want to drop exceptions on the floor and pretend they didn't happen. I'm willing to accept that you have some good justification for doing so. But the fact is, in the general case, it's not a good idea. Most programmers don't want to do it, and there are good arguments to say that it is generally unwise to do this sort of thing.
A good language will let you do something unwise. But a good language will not let you do something unwise by accident. Since the behavior you want is generally unwise, languages tend to make you explicitly request it.
FWIW,if
does require anelse
in Haskell since it's not possible to "do nothing".
– immibis
1 hour ago
add a comment |
I don't need the catch block.
But you do need to catch. The behavior of your code with a catch block is to catch any exception, and then forget that it happened. So any exception that tries to pass through will stop, and your code will basically pretend that the try
block executed successfully.
So you want a naked try
block to act like it catches an exception and ignores it. Here's the thing: a default case is meant to be a common case, one that is useful by many users and not error prone. if
doesn't typically require an else
because there are many cases where you have nothing to do.
I know nothing about why you want to drop exceptions on the floor and pretend they didn't happen. I'm willing to accept that you have some good justification for doing so. But the fact is, in the general case, it's not a good idea. Most programmers don't want to do it, and there are good arguments to say that it is generally unwise to do this sort of thing.
A good language will let you do something unwise. But a good language will not let you do something unwise by accident. Since the behavior you want is generally unwise, languages tend to make you explicitly request it.
FWIW,if
does require anelse
in Haskell since it's not possible to "do nothing".
– immibis
1 hour ago
add a comment |
I don't need the catch block.
But you do need to catch. The behavior of your code with a catch block is to catch any exception, and then forget that it happened. So any exception that tries to pass through will stop, and your code will basically pretend that the try
block executed successfully.
So you want a naked try
block to act like it catches an exception and ignores it. Here's the thing: a default case is meant to be a common case, one that is useful by many users and not error prone. if
doesn't typically require an else
because there are many cases where you have nothing to do.
I know nothing about why you want to drop exceptions on the floor and pretend they didn't happen. I'm willing to accept that you have some good justification for doing so. But the fact is, in the general case, it's not a good idea. Most programmers don't want to do it, and there are good arguments to say that it is generally unwise to do this sort of thing.
A good language will let you do something unwise. But a good language will not let you do something unwise by accident. Since the behavior you want is generally unwise, languages tend to make you explicitly request it.
I don't need the catch block.
But you do need to catch. The behavior of your code with a catch block is to catch any exception, and then forget that it happened. So any exception that tries to pass through will stop, and your code will basically pretend that the try
block executed successfully.
So you want a naked try
block to act like it catches an exception and ignores it. Here's the thing: a default case is meant to be a common case, one that is useful by many users and not error prone. if
doesn't typically require an else
because there are many cases where you have nothing to do.
I know nothing about why you want to drop exceptions on the floor and pretend they didn't happen. I'm willing to accept that you have some good justification for doing so. But the fact is, in the general case, it's not a good idea. Most programmers don't want to do it, and there are good arguments to say that it is generally unwise to do this sort of thing.
A good language will let you do something unwise. But a good language will not let you do something unwise by accident. Since the behavior you want is generally unwise, languages tend to make you explicitly request it.
edited 1 hour ago
answered 3 hours ago
Nicol BolasNicol Bolas
9,89042738
9,89042738
FWIW,if
does require anelse
in Haskell since it's not possible to "do nothing".
– immibis
1 hour ago
add a comment |
FWIW,if
does require anelse
in Haskell since it's not possible to "do nothing".
– immibis
1 hour ago
FWIW,
if
does require an else
in Haskell since it's not possible to "do nothing".– immibis
1 hour ago
FWIW,
if
does require an else
in Haskell since it's not possible to "do nothing".– immibis
1 hour ago
add a comment |
What would that mean?
I would expect:
try
const utils = require("applicationutils");
To mean the same thing as:
const utils = require("applicationutils");
try/catch/finally is a well understood pattern, used in a lot of different languages. Exceptions by their very nature imply that there is a recovery handler somewhere else.
It sounds like what you are looking for is VB’s On Error Resume Next, which catches and ignores errors on a line by line basis. Today this is generally considered a bad practice, because it means that there is no actual error handling going on — flow continues as if the previous line had succeeded, which can lead to escalating the level of garbage/damage that is done by bad input, instead of halting or eliminating just the bad input.
The try/catch pattern is useful, because it can segment your algorithm into parts where you can fail but recover, and parts where there is no recovery after failure. And in the event that there is no recovery after failure, it is that part that cannot continue not the rest of the application.
If you have a need for this in js, you could get the same effect, by creating a function that takes a function as it’s argument, wraps it in a executes it inside a try catch block where it just sets an error object. You could then use lambda expressions for any line you would like to resume next on.
var err =
var ex = function(action)
try
action()
catch (e)
err=e
var i =5
ex(t=> alert(i.foo()))
console.log(i)
console.log(err.message)
Although it won’t work with lines that declare and initialize variables.
Look at the example on the question. OP does not wantsetting
,myRegEx
andsupportsRegEx2
to be modified ifrequire
fails. It is not a simpleOn Error Resume Next
. It would be more likeOn Error Goto Label
. Addendum: ultimately what OP wants is to omitcatch(e)
but have it behave exactly as if it were there.
– Theraot
11 mins ago
add a comment |
What would that mean?
I would expect:
try
const utils = require("applicationutils");
To mean the same thing as:
const utils = require("applicationutils");
try/catch/finally is a well understood pattern, used in a lot of different languages. Exceptions by their very nature imply that there is a recovery handler somewhere else.
It sounds like what you are looking for is VB’s On Error Resume Next, which catches and ignores errors on a line by line basis. Today this is generally considered a bad practice, because it means that there is no actual error handling going on — flow continues as if the previous line had succeeded, which can lead to escalating the level of garbage/damage that is done by bad input, instead of halting or eliminating just the bad input.
The try/catch pattern is useful, because it can segment your algorithm into parts where you can fail but recover, and parts where there is no recovery after failure. And in the event that there is no recovery after failure, it is that part that cannot continue not the rest of the application.
If you have a need for this in js, you could get the same effect, by creating a function that takes a function as it’s argument, wraps it in a executes it inside a try catch block where it just sets an error object. You could then use lambda expressions for any line you would like to resume next on.
var err =
var ex = function(action)
try
action()
catch (e)
err=e
var i =5
ex(t=> alert(i.foo()))
console.log(i)
console.log(err.message)
Although it won’t work with lines that declare and initialize variables.
Look at the example on the question. OP does not wantsetting
,myRegEx
andsupportsRegEx2
to be modified ifrequire
fails. It is not a simpleOn Error Resume Next
. It would be more likeOn Error Goto Label
. Addendum: ultimately what OP wants is to omitcatch(e)
but have it behave exactly as if it were there.
– Theraot
11 mins ago
add a comment |
What would that mean?
I would expect:
try
const utils = require("applicationutils");
To mean the same thing as:
const utils = require("applicationutils");
try/catch/finally is a well understood pattern, used in a lot of different languages. Exceptions by their very nature imply that there is a recovery handler somewhere else.
It sounds like what you are looking for is VB’s On Error Resume Next, which catches and ignores errors on a line by line basis. Today this is generally considered a bad practice, because it means that there is no actual error handling going on — flow continues as if the previous line had succeeded, which can lead to escalating the level of garbage/damage that is done by bad input, instead of halting or eliminating just the bad input.
The try/catch pattern is useful, because it can segment your algorithm into parts where you can fail but recover, and parts where there is no recovery after failure. And in the event that there is no recovery after failure, it is that part that cannot continue not the rest of the application.
If you have a need for this in js, you could get the same effect, by creating a function that takes a function as it’s argument, wraps it in a executes it inside a try catch block where it just sets an error object. You could then use lambda expressions for any line you would like to resume next on.
var err =
var ex = function(action)
try
action()
catch (e)
err=e
var i =5
ex(t=> alert(i.foo()))
console.log(i)
console.log(err.message)
Although it won’t work with lines that declare and initialize variables.
What would that mean?
I would expect:
try
const utils = require("applicationutils");
To mean the same thing as:
const utils = require("applicationutils");
try/catch/finally is a well understood pattern, used in a lot of different languages. Exceptions by their very nature imply that there is a recovery handler somewhere else.
It sounds like what you are looking for is VB’s On Error Resume Next, which catches and ignores errors on a line by line basis. Today this is generally considered a bad practice, because it means that there is no actual error handling going on — flow continues as if the previous line had succeeded, which can lead to escalating the level of garbage/damage that is done by bad input, instead of halting or eliminating just the bad input.
The try/catch pattern is useful, because it can segment your algorithm into parts where you can fail but recover, and parts where there is no recovery after failure. And in the event that there is no recovery after failure, it is that part that cannot continue not the rest of the application.
If you have a need for this in js, you could get the same effect, by creating a function that takes a function as it’s argument, wraps it in a executes it inside a try catch block where it just sets an error object. You could then use lambda expressions for any line you would like to resume next on.
var err =
var ex = function(action)
try
action()
catch (e)
err=e
var i =5
ex(t=> alert(i.foo()))
console.log(i)
console.log(err.message)
Although it won’t work with lines that declare and initialize variables.
edited 1 hour ago
answered 1 hour ago
jmorenojmoreno
8,96512244
8,96512244
Look at the example on the question. OP does not wantsetting
,myRegEx
andsupportsRegEx2
to be modified ifrequire
fails. It is not a simpleOn Error Resume Next
. It would be more likeOn Error Goto Label
. Addendum: ultimately what OP wants is to omitcatch(e)
but have it behave exactly as if it were there.
– Theraot
11 mins ago
add a comment |
Look at the example on the question. OP does not wantsetting
,myRegEx
andsupportsRegEx2
to be modified ifrequire
fails. It is not a simpleOn Error Resume Next
. It would be more likeOn Error Goto Label
. Addendum: ultimately what OP wants is to omitcatch(e)
but have it behave exactly as if it were there.
– Theraot
11 mins ago
Look at the example on the question. OP does not want
setting
, myRegEx
and supportsRegEx2
to be modified if require
fails. It is not a simple On Error Resume Next
. It would be more like On Error Goto Label
. Addendum: ultimately what OP wants is to omit catch(e)
but have it behave exactly as if it were there.– Theraot
11 mins ago
Look at the example on the question. OP does not want
setting
, myRegEx
and supportsRegEx2
to be modified if require
fails. It is not a simple On Error Resume Next
. It would be more like On Error Goto Label
. Addendum: ultimately what OP wants is to omit catch(e)
but have it behave exactly as if it were there.– Theraot
11 mins ago
add a comment |
Thanks for contributing an answer to Software Engineering Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f391999%2fis-there-a-language-that-lets-you-use-a-try-block-without-a-catch-block%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you explain why you want a try without a catch-block?
– Pieter B
4 mins ago