Wrap Error and Print Chain
JavaScript
Medium
4 views
Problem Description
One word is given. If it is NEG, throw error NEG inside inner(). Outer should catch it, wrap as OUTER and attach cause. Finally print messages chain like OUTER->NEG.
Output Format
One line chain output.
Constraints
Word length up to 1e5.
Official Solution
const fs=require('fs');const w=fs.readFileSync(0,'utf8').trim();if(!w)process.exit(0);const inner=()=>{if(w==='NEG')throw new Error('NEG');return 'OK';};try{try{inner();process.stdout.write('OK');}catch(e){const err=new Error('OUTER');err.cause=e;throw err;}}catch(e){let cur=e;let parts=[];while(cur){parts.push(cur.message||'ERR');cur=cur.cause;}process.stdout.write(parts.join('->'));}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!